PDA

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




iJustinCabral
Aug 21, 2012, 09:06 AM
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.



robbieduncan
Aug 21, 2012, 09:19 AM
What have you tried? What didn't work? Are these C arrays? NSArrays? Something else?

iJustinCabral
Aug 21, 2012, 10:18 AM
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.

robbieduncan
Aug 21, 2012, 10:20 AM
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 (http://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CF8QFjAA&url=http%3A%2F%2Fdeveloper.apple.com%2Fdocumentation%2FCocoa%2FReference%2FFoundation%2FClasses%2FNS MutableArray_Class%2F&ei=JqczUPm-Ne-S0QXZkIH4Bg&usg=AFQjCNEfDZCbJtqyOXbTUut7KH5thdijPA) and look for methods to add an object and remove an object at a specified index.

iJustinCabral
Aug 21, 2012, 10:31 AM
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?

robbieduncan
Aug 21, 2012, 10:35 AM
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?

Yes something like that sounds like a good approach

iJustinCabral
Aug 21, 2012, 10:43 AM
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)

robbieduncan
Aug 21, 2012, 10:49 AM
What is the purpose of the IndexPath? What type of parameter does objectAtIndex: expect?

iJustinCabral
Aug 21, 2012, 10:55 AM
Nice..with what you said I realized I didn't need a NSIndexPath.

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

robbieduncan
Aug 21, 2012, 11:01 AM
Nice..with what you said I realized I didn't need a NSIndexPath.

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

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.

iJustinCabral
Aug 21, 2012, 11:51 AM
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?

Reason077
Aug 21, 2012, 12:31 PM
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.


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.

iJustinCabral
Aug 21, 2012, 02:13 PM
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.

chown33
Aug 21, 2012, 03:05 PM
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.

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.

Reason077
Aug 21, 2012, 04:47 PM
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.

Duncan C
Aug 22, 2012, 06:56 AM
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?

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.)

iJustinCabral
Aug 22, 2012, 09:33 AM
^^^^ 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.

robbieduncan
Aug 22, 2012, 09:48 AM
^^^^ 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.

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

ArtOfWarfare
Aug 22, 2012, 11:25 AM
^^^^ 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.

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.