1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

How can I take a random object from one array, and insert to another? **Help**

Discussion in 'iPhone/iPad Programming' started by iJustinCabral, Aug 21, 2012.

  1. macrumors member


    I know about using something like

    NSUInteger randomIndex = arc4random() % [theList count];

    but I'm not sure how to set up the syntax between arrays to use something like that.

    I also want to make it so the array from which the random object is taken, removes the object that is taken.

    Can anyone out there help me out? Thanks in advance.
  2. Moderator


    Staff Member

    What have you tried? What didn't work? Are these C arrays? NSArrays? Something else?
  3. macrumors member


    This is what i was trying

    I have two arrays

    NSMutableArray *theList;
    NSMutableArray *viewList;

    -(IBAction)tapButton:(UIButton *)button
    if (!.self.viewList)
    self.viewList [[NSMutableArray alloc] init];
    [hatList addObjectsFromArray:theList];
    [theList removeAllObjects];
    [self.collectionView reloadData];
    I can't figure how to implement that arc4random in this situation.
  4. Moderator


    Staff Member

    You can't using the methods you posted. You are adding all objects from one array then removing them from the source array. I suggest you look at the NSMutableArray documentation and look for methods to add an object and remove an object at a specified index.
  5. macrumors member


    I was going to try something like

    Paper *paperObject = [theList objectAtIndex:indexPath.row];

    then just add the paper object to on array then remove from the other?
  6. Moderator


    Staff Member

    Yes something like that sounds like a good approach
  7. macrumors member


    Heres what I have now but now I need to convert this NSUInteger for my NSIndexPath

        if (!self.viewList)
            self.viewList = [[NSMutableArray alloc]init];
            NSLog(@"viewList has been created");
        NSUInteger randomIndex = arc4random() % [theList count];
        NSIndexPath *paperIndex = [NSIndexPath indexPathWithIndex:randomIndex];
        Paper *paperObject = [theList objectAtIndex:paperIndex];
        [hatList addObject:paperObject];
        [theList removeObject:paperObject];
        [self.collectionView reloadData];
    I get this error "Incompatible pointer to integer conversion sending "NSIndexPath *__strong" to paramater of type 'NSUInteger'(AKA unsigned int)
  8. Moderator


    Staff Member

    What is the purpose of the IndexPath? What type of parameter does objectAtIndex: expect?
  9. macrumors member


    Nice..with what you said I realized I didn't need a NSIndexPath.

    Thanks so much! Helping me fly through my debug stage.
  10. Moderator


    Staff Member

    Great :) Hopefully you can take this approach to the next problem. The more you do the less you'll find that you need help as the documentation will solve most of your problems.
  11. macrumors member


    A problem I'm getting now is I cant get my cell to display the name of the text that was written into the object.

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
        IRHomeCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"IRHomeCell" forIndexPath:indexPath];
        NSLog(@"in cell for row");
        Paper *paperObject = [hatList objectAtIndex:indexPath.row];
        cell.name = (UILabel *)[cell viewWithTag:10];
        cell.number = (UILabel *)[cell viewWithTag:15];
        cell.name.text = paperObject.listNumber;
        cell.number.text = paperObject.listNumber;
        cell.backgroundColor = [UIColor whiteColor];
        [cell setNeedsDisplay];
        return cell;
    The cell displays just a white background every time I launch, it's just the name that I can't seem to get to show up.

    Any ideas?
  12. macrumors 68000


    You need to check for a nil result from dequeueReusableCellWithReuseIdentifier, and alloc a new cell if it didn't give you a reusable (cached) one.
  13. iJustinCabral, Aug 21, 2012
    Last edited: Aug 21, 2012

    macrumors member


    I thought with iOS 6 sdk CollectionViews & TableViews now you dont have to do that.

    Instead you just register your cell class in the ViewControllers viewDidLoad.

    Atleast thats what I thought.

    I'll try your theory though and post back.
  14. macrumors 603

    Debugging is about testing your assumptions. That means breaking things down into individual testable assumptions, then testing them.

    This is why learning to use a debugger is important. It lets you stop the program at a breakpoint, look at the actual values in variables, and step through. At each step (break things down), you look at the actual values (test your assumption) to see if it's correct (does the value match the assumption).

    Even debugging with NSLog follows this pattern. Break things down into individual values (variables) you need to test. Then print the value. Printing only one NSLog for an entire sequence, and not even printing a variable, is not breaking things down. All you know is whether it reached that method or not. You know nothing about what's actually happening inside the method.

    Learning a programming language without learning to use a debugger is like learning to fly by only learning how to take off, and not learning how to land. You have lots of crashes and no way to learn anything useful from your mistakes.

    Learn to use the debugger now, on relatively simple code. It will actually make it easier to understand what your code is really doing, which means it's easier to learn how to write better code.
  15. macrumors 68000


    Ahh, ok. You're using dequeueReusableCellWithReuseIdentifier:forIndexPath. This is new in iOS 6, and indeed shouldn't return nil.

    First I'd check if it is indeed returning a valid cell. If not, maybe your cell class is not being registered properly. If it is returning a cell, there may be something wrong with your UITableViewCell implementation.
  16. macrumors 6502a

    Duncan C

    The code above does't make any sense to me. Can you post the definition of IRHomeCell?

    At a glance, the code that assigns values to cell.name, cell.number, etc, seems odd. You're assigning objects to properties of the cell that come FROM the cell. What is that code supposed to do, set up the outlets for the cell object from views marked with tags? You can do that in the nibfile for the cell. Make the "files owner" for the XIB be the cell object itself, and link up the outlets to the appropriate views inside the cell, just like you do with a view controller.

    Are you sure that [cell viewWithTag: x] is returning a value? For table views, you need to use [cell.contentView viewWithTag: x]

    I've only looked at iOS 6 Collection views a little, and am not allowed to talk about them here in specifics because they are still under NDA.

    (I don't understand how this forum can allow discussion of beta Apple OS'es, since our developer agreements expressly ban us from disclosing information about beta software releases to non-NDA recipients, and this is a public forum.)
  17. macrumors member


    ^^^^ Really??

    Just a up and coming developer looking for help. No need to take this so serious man. Didn't realize I couldn't talk about whats in the SDK.
  18. Moderator


    Staff Member

    He is correct. You should probably read the contract you signed with Apple!
  19. macrumors 603


    Discuss such features on Apple's own forums until the SDK is released. The NDA pretty clearly states that you shouldn't talk about it on a publicly accessible forum like this one.

Share This Page