Create an unknown number of objects - iphone

I need to create a specific number instances of an object based on a variable. so the pseudo code looks kinda like this
for(int x; x < aInt; x++) {
//create object and initialize it
}
how would I do that and create a different object each time with a different name and memory location?

Stick the reference in a NSMutableArray (or a NSArray, given that you appear to know the size in advance).
NSMutableArray *array = [[NSMutableArray alloc]init]
for(int x; x < aInt; x++) {
//create object and initialize it
YourObject *o = [[YourObject alloc]init];
[array addObject:o];
[o release];
}
// do whatever you need to do with the objects
A NSDictionary/NSMutableDictionary is certainly an option as well, depending on what your requirements are.

Just use a NSMutableArray:
NSMutableArray *objectsArray = [NSMutableArray arrayWithCapacity:(YourIntegerValue)];
for(int x; x < aInt; x++) {
//create object and initialize it
[objectsArray addObject:(YourObject)];
}
Don't forget to release the array after you're done working with it!

Related

NSMutableArray of NSMutableArrays. Mutating method sent to immutable object

I have NSMutableArray of NSMutableArrays:
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i = 0; i < 5; i++)
{
NSMutableArray *miniArray = [[NSMutableArray alloc]init];
for (int k = 0; k < 30; k++)
{
[miniArray addObject:#"0"];
}
[array addObject:miniArray];
}
Then, when I try to do this:
[[array objectAtIndex:packIndex]replaceObjectAtIndex:index withObject:#"1"];
it crashes with: [__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
Why ? How to fix ? Thanks !
UPD:
I save this array in NSUserDefaults:
[defaults setObject:array forKey:#"mainArray"];
Then, I read it in the other class:
array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"mainArray"]];
Also, I must to mention that sometimes the code runs well and it changes "0" to "1". But it also crashes sometimes. So I cant see the logic, why it works fine or why it crashes sometimes.
The problem is that when you read the array out of NSUserDefaults, the mini-arrays are not automatically NSMutableArrays.
Try this:
array = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"mainArray"]];
for(int i = 0; i < array.count; i++) {
NSArray * tempArray = array[i];
array[i] = [tempArray mutableCopy];
}
Edit:
Best Coder's answer explains why this is.
Objects stored in NSUserDefaults are stored as immutable versions, basically NSUserDefaults is a plist and there is no flag marking an array as mutable/immutable so when you read them back out, they are assumed to be immutable.
Values returned from NSUserDefaults are immutable, even if you set a
mutable object as the value. For example, if you set a mutable string
as the value for "MyStringDefault", the string you later retrieve
using stringForKey: will be immutable.
Instead, make a mutableCopy of the array you retrieve from NSUserDefaults, add your object, then set your new array back in.
see this link:
https://developer.apple.com/documentation/foundation/nsuserdefaults
Try using more verbose code:
NSMutableArray *tempArray = [array objectAtIndex:packIndex];
[tempArray replaceObjectAtIndex:index withObject:#"1"];

Put all object properties into an NSDictionary or NSArray

Is there a way to automatically return an NSDictionary of all the (public) properties in a class? You can assume all properties are property lists and that I don't want to just hand over a pointer to the class itself. Any existing magic that can pull this off?
I know I can lazy instantiate an NSDictionary and manually fill it with properties in the getter.
It's easy to get an array of declared properties using the class_copyPropertyList and property_getName functions in the Objective-C runtime. Here's one such implementation:
- (NSArray *)properties
{
NSMutableArray *propList = [NSMutableArray array];
unsigned int numProps = 0;
unsigned int i = 0;
objc_property_t *props = class_copyPropertyList([TestClass class], &numProps);
for (i = 0; i < numProps; i++) {
NSString *prop = [NSString stringWithUTF8String:property_getName(props[i])];
[propList addObject:prop];
}
return [[propList copy] autorelease];
}
You could add this as a method in a category on NSObject. Here's a full code listing that can be compiled that demonstrates this.
I'm not sure how'd you do it with an NSDictionary, only because I'm not sure what you expect the key-value pairs to be.

Initialize an NSArray with the numbers in a sequential manner when the count of the array is known

I want to initialize an NSArray with numbers starting from 0,1,2,3... I know the count of the array. For example:
I have an array that needs to be initialized with 5 (count) as capacity. Now I want to initialize this array with 0,1,2,3,4 and I need to initialize it dynamically.
If the count of the array is 10, I need to initialize the array with 0,1,2,3,4,5,6,7,8,9 at respective indexes. The problem is that count of the array changes dynamically and I need to initialize it accordingly.
Can someone suggest me any idea on how to implement this?
NSMutableArray* arrOfObject = [[NSMutableArray alloc] init];
for(int i=0; i< [arr count]; i++)
{
arrOfObject addObject:[NSNumber numberWithInt:i];
}
Initialize a mutable array. Then add the numbers in a loop. Optionally initialize a new non-mutable array with your mutable one using arrayWithArray:.
int count = 5;//suppose this you want
NSMutableArray *array = [NSMutableArray array];
for(int i=0 ; i< count; i++) {
[array addObject:[NSNumber numberWithInt:i];
}
You just need to pass the count from where you want it, and it will dynamically generate your desired array.
Use NSMutableArray then just add as many as you need
int count = 10;
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i=0;i<count;i++) {
[array addObject:[NSNumber numberWithInt:i]];
}

Loading values into an array from two different arrays iphone sdk

Here I am having a situation, I'm using the following code:
int x=0;
for (int i=0; i<=[arrayDeals count]-1; i++) {
x++;
//NSString *deal = [arrayDeals objectAtIndex:i];
combinedArr = [[NSMutableArray alloc]initWithObjects:
[CustomObject customObjectWithName:[arrayDeals objectAtIndex:i] andNumber:x],nil];
}
I need to load the values from arrayDeals and the 'x' value into combinedArr. So, I put this in a for loop. But i got only one value from each arrays. What is went wrong here? Please help me. (here CustomObject is a NSObject)
Thank you.
Well there are many things wrong with the code you posted, but I think this is what you want:
int x = 0;
NSMutableArray *combinedArr = [[NSMutableArray alloc] init]:
NSInteger count = [arrayDeals count];
for (int i = 0; i < count; i++) {
x++;
CustomObject *customObject = [CustomObject customObjectWithName:[arrayDeals objectAtIndex:i] andNumber:x];
[combinedArr addObject:customObject];
}
To give you some idea of what is wrong with the code you posted:
combinedArr = [[NSMutableArray alloc]initWithObjects:
[CustomObject customObjectWithName:[arrayDeals objectAtIndex:i] andNumber:x],nil];
Here you create a new NSMutableArray to which you assign an new object to taked the object from the array arrayDeals. But you create this NSMutableArray for every item in the array arrayDeals and you assign them to the same variable.
So each iteration you leak the NSMutableArray.
Also :
for (int i=0; i<=[arrayDeals count]-1; i++) {
is the same as
for (int i=0; i < [arrayDeals count]; i++) {
but the count is called every time you iterate, so as per my example I saved the count in a int to just speed things up.
You could even speed the code up using fast Enumeration:
NSInteger x = 0;
NSMutableArray *combinedArr = [[NSMutableArray alloc] init]:
for (id object in arrayDeals) {
id secondObject = [secondArray itemAtIndex:x];
// Arrays start at 0 so only up it after we've got the object.
x++;
CustomObject *customObject = [CustomObject customObjectWithName:object andNumber:x];
[combinedArr addObject:customObject];
}

float* array to NSArray, iOS

I have a float pointer array and I would like to convert it to an NSArray.
Is there a better way to do it than to iterate through the float* and add each entry to the NSArray?
I have:
float* data = new float[elements];
fill up data from binary ifstream
I want to avoid doing something like:
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:elements];
for (int i=0;i<elements;i++)
{
[mutableArray addObject:[NSNumber numberWithFloat:data[i]]];
}
NSArray *array = [NSArray arrayWithArray:array];
Is there some convenience / more efficient method to copy a large chunk of floats into an NSArray?
Regards,
Owen
You’ve got two problems: first, you can’t store a float in an NSArray, since NSArrays will only hold Objective-C objects. You’ll need to wrap then in an object, probably NSNumber or NSValue.
As to your original question, since you have to create the objects anyway, there isn’t a better method. I’d recommend the for loop:
for (int i = 0; i < elements; i++) {
NSNumber *number = [NSNumber numberWithFloat:floatArray[i]];
[myArray addObject:number];
}
Keep in mind that number will be autoreleased. If you’re dealing with a lot of numbers, that can get out of hand pretty quickly with memory management, so you might do this instead:
for (int i = 0; i < elements; i++) {
NSNumber *number = [[NSNumber alloc] initWithFloat:floatArray[i]];
[myArray addObject:number];
[number release];
}