Fast enumeration-array - ios

Hi I'm trying to loop through an array of objects in an array to try and assign them an value. Im doing this through fast enumeration but when I run the build it succeeds but crashes and points to this line:
for (SKSpriteNode* var in objects_array)
. Did i mess up the syntax? I'm still new to objective-c. It tells me that var is unused and when i run the build that point is a breakpoint.
(btw I didn't include the code for when I created the actual SKSpriteNode objects left middle and right because they have multiple properties and i thought it may be distracting. I can post it though if needed)
Thanks!
NSMutableArray* objects_array = [[NSMutableArray alloc] init];
NSMutableArray* value_array = [[NSMutableArray alloc] init];
[objects_array addObject:#"left"];
[objects_array addObject:#"middle"];
[objects_array addObject:#"right"];
for (SKSpriteNode* var in objects_array) {
int value =arc4random_uniform(1);
[value_array addObject:[NSNumber numberWithInt:value]];
}

You have two problems. The first one was said by Rory; you forgot to initialize your variables:
NSMutableArray* objects_array = [[NSMutableArray alloc] init];
NSMutableArray* value_array = [[NSMutableArray alloc] init];
The second problem is that you are working with different variables. Here you say that you are working with NSString*:
[objects_array addObject:#"left"];
[objects_array addObject:#"middle"];
[objects_array addObject:#"right"];
And here you say that you are working with SKSpriteNode*:
for (SKSpriteNode* var in objects_array)
If your sprites are called left, middle and right, you should do this to initialize them (for example):
[SKSpriteNode spriteNodeWithImageNamed:#"left.png"];
And in that case, that should be your for:
for (NSString* var in objects_array)

You need to initialise the arrays:
NSMutableArray* objects_array = [[NSMutableArray alloc] init];
NSMutableArray* value_array = [[NSMutableArray alloc] init];

Related

add an array to another array in iOS

I have a strange problem because "addObject" is working to add an NSString but not to add an NSArray:
NSMutableString *starCatalogEntryValue = [[NSMutableString alloc] init]; // a single string from a catalog
NSMutableArray *starCatalogEntryData = [[NSMutableArray alloc] init]; // array of strings
NSMutableArray *starCatalogData = [[NSMutableArray alloc] init]; // array of arrays
loop i times {
[starCatalogEntryData removeAllObjects];
loop j times {
[starCatalogEntryData addObject:starCatalogEntryValue]; // This works
}
[starCatalogData addObject:starCatalogEntryData]; // This does not work
}
Actually, adding the array starCatalogEntryData works but not properly. I end up with i entries in starCatalogData but they are all equal to the last value of starCatalogEntryData.
The problem is that you reuse startCatalogEntryData over and over. You want this:
NSMutableString *starCatalogEntryValue = [[NSMutableString alloc] init]; // a single string from a catalog
NSMutableArray *starCatalogData = [[NSMutableArray alloc] init]; // array of arrays
loop i times {
NSMutableArray *starCatalogEntryData = [[NSMutableArray alloc] init]; // array of strings
loop j times {
[starCatalogEntryData addObject:starCatalogEntryValue]; // This works
}
[starCatalogData addObject:starCatalogEntryData]; // This does not work
}
This creates a new array each time.

Objective C removeObjectsAtIndexes desire cleaner coding

I would like to create a shorter and more elegant version of some working code.
FYI I have to add an element to an existing array, and then remove one, and repeat. (unfortunately I can't add more than one element and then remove more than one).
Working code:
NSMutableArray *destinationArray;
destinationArray = [[NSMutableArray alloc] init];
NSMutableArray *originArray = [[NSMutableArray alloc]init];
// Copy one specific array element from originArray to destinationArray
// Note: assume originArray and destinationArray populated at this point
int originIndex = var1-var2+1;
[destinationArray addObject:originArray[originIndex]];
// Remove one specific array element from destinationArray
[destinationIndex removeAllIndexes]; // clear index if used previously
[destinationIndex addIndex:var3-var4-1];
[destinationArray removeObjectsAtIndexes:destinationIndex];
I would prefer some version of the following:
[destinationArray addObject:originArray[var1-var2+1]];
[destinationArray removeObjectsAtIndexes:[var3-var4-1]];
Is there a way to make this shorter version work? (gives expected identifier error)
Thanks much for your help...
Why not do this:
NSMutableArray *destinationArray = [[NSMutableArray alloc] init];
NSMutableArray *originArray = [[NSMutableArray alloc]init];
// somewhere in here objects are added to "originArray"
[destinationArray addObject:originArray[var1-var2+1]];
[destinationArray removeObjectAtIndex:var3-var4-1];

NSMutableArray does not add objects [duplicate]

This question already has answers here:
NSMutableArray addObject not working
(2 answers)
Cannot add object to an NSMutableArray array
(2 answers)
Closed 10 years ago.
I think, I am doing a pretty basic mistake, but I am using an NSMutableArray and this somehow doesn't add the object, I'm sending it its way. I have a property (and synthesize)
#property (nonatomic, strong) NSMutableArray *kpiStorage;
and then:
ExampleObject *obj1 = [[ExampleObject alloc] init];
[kpiStorage addObject:obj1];
ExampleObject *obj2 = [[ExampleObject alloc] init];
[kpiStorage addObject:obj2];
NSLog(#"kpistorage has:%#", [kpiStorage count]);
and that always returns (null) in the console. What am I misunderstanding?
Make sure you allocated memory for kpiStorage.
self.kpiStorage = [[NSMutableArray alloc] init];
On top of forgetting to allocated memory for your NSMutableArray, your NSLog formatting is also wrong. Your app will crash when you run it. The following changes are needed
You will need to add
self.kpiStorage = [[NSMutableArray alloc] init];
and change your NSLog to the following
NSLog(#"kpistorage has:%d", [self.kpiStorage count]);
If you are not using ARC make sure you should not create a memory leak in your project. So better way would be allocating like this
NSMutableArray *array = [[NSMutableArray alloc] init];
self.kpiStorage = array;
[array release];
make it a habit to do not directly do
self.kpiStorage = [[NSMutableArray alloc] init];
in this case your property's retain count is incremented by 2. For further reading you can stydy Memory Leak when retaining property

iOS: Difference between NSMutableArray alloc/init and arrayWithObjects:

What is the difference between
NSMutableArray* p = [[NSMutableArray alloc] initWithObjects:...]
and
NSMutableArray* p = [NSMutableArray arrayWithObjects:...]
In the first one, you have the ownership of array object & you have to release them.
NSMutableArray* p = [[NSMutableArray alloc] initWithObjects:...];
[p release];
& last one you dont need to release as you don't have the ownership of array object.
NSMutableArray* p = [NSMutableArray arrayWithObjects:...]; //this is autoreleased
If you call release in this, then it will crash your application.
[NSMutableArray arrayWithObjects:] is the same as [[[NSMutableArray alloc] initWithObjects:] autorelease]
In practice, there is no difference if you're on ARC.
The latter basically is just a shorthand for [[NSMutableArray alloc] initWithObjects: ...], except the returned array is autoreleased (which is important if you're still doing manual reference counting).
What I think the difference is that: initWithObjects is a instance method, and arrayWithObject is a class method.

Anything in iPhone that resembles Android ArrayList

I am from android background and I just started working on iPhone
I want to perform this operation in iPhone as I do in Android.
ArrayList<String> aa = new ArrayList<String>();
public void fillArray(String s)
{
aa.add(s);
}
As Binyamin Sharet suggest you have to use NSMutableArray. This class allows you to create a dynamic array. You can perform addition or deletion. On the contrary NSArray is a immutable version of it. You cannot add or delete objects to a NSArray once created.
The same distinction can be applied to NSDictionary and NSMutableDictionary (and other).
Here a simple example.
NSMutableArray* arr = [[NSMutableArray alloc] init];
[arr addObject:#"first string"];
[arr addObject:#"second string"];
An important aspect of NSMutableArray (the same could be applied to other class) is the memory management one. When you add an object to a NSMutableArray it retains objects added to it. So, if you NOT use ARC you have to deal with this aspect.
NSMutableArray* arr = [[NSMutableArray alloc] init];
id obj = [[NSObject alloc] init]; // a general object
[arr addObject:obj];
[obj release];
For further info I suggest you to read about NSMutableArray class reference.
Hope it helps.

Resources