How to put in array a different types of field in objective-c - objective-c

I'm writing an app for iPhone in objective-c. I want to declare an array that will hold different type of fields , like: int, NSString, bool.
Can I do it?

You can put whatever items in an NSArray as long as they are objects. So you have to wrap items that are not objects (such as BOOL, int and CGPoint) in some kind of objects such as NSNumber or NSValue.
NSMutableArray *array = [[NSMutableArray] alloc] init];
[array addObject:myString];
[array addObject:[NSNumber numberWithInt:1]];
[array addObject:[NSNumber numberWithFloat:1.0]];
[array addObject:[NSValue valueWithPoint:myPoint]]; // myPoint is a CGPoint
[array addObject:[NSValue valueWithRect:myRect]]; // myRect is a CGRect

Yes, you can
NSMutableArray *array = [NSMutableArray array];
NSString *string = #"str";
[array addObject:string]; //string
NSNumber *num = [NSNumber numberWithInt:1];
[array addObject:num]; //int
NSNumber *boolNum = [NSNumber numberWithBool:YES];
[array addObject:boolNum]; //bool

Use NSMutableArray.
NSMutableArray *array = [[NSMutableArray alloc]init];
Now use addObject: method to add objects. for adding int, bool value create NSNumber object.

It is possible. Just create the array and add the objects you want added.

Related

Seg Fault when using addObject to NSMutableArray

I seem to be having a problem with the NSMutableArray.
Here's my code:
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
while(condition) {
NSInteger temp = someNumber;
[returnArray addObject: temp];
}
But as soon as it hits the addObject message, the program seg faults. Any advice?
You can't add primitives like integers to an array, only objects (hence the name addObject:). If you want to add numbers, you have to convert them to an NSNumber, or one of the related classes.
You can only add objects to array and NSInteger is not an array.
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
while(condition) {
[returnArray addObject: [NSNumber numberWithInt: someNumber]];
}
You need to wrap primitives such as NSInteger into an NSNumber class. You can do the following:
while(condition)
{
NSInteger temp = someNumber;
[returnArray addObject:#(temp)];
}
Or if your compiler doesn't support that syntax:
while(condition)
{
NSInteger temp = someNumber;
[returnArray addObject:[NSNumber numberWithInteger:temp]];
}

Assigning an arrays count to an int, NSNumber or NSUInteger fails

Assigning an int, NSNumber and NSUInteger to an arrays count fails...
this is my line of code
int diamondCount = [diamonds count];
where diamonds is an nsarray
I built the app and it threw an EXC_BAD_ACESS on that line
So i looked at the NSArray count method and found it returns an nsuinteger
So i did:
NSUInteger diamondCount = [diamonds count];
that still threw me EXC_BAD_ACCESS so at last resort i assigned it to an nsnumber with numberWithInteger:
it stilled crashed.... any thoughts?
diamonds does not point to a real (and valid) NSArray. Check its initialization.
Try this:
NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = #"a string";
myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
int i = [myArray count];
NSLog(#"%d",i);
NSInteger j = [myArray count];
NSLog(#"%d",j);
NSNumber *k = [myArray count];
NSLog(#"%d",k);
/*output
2011-11-21 17:01:48.272 test[48840:1303] 3
2011-11-21 17:01:48.272 test[48840:1303] 3
2011-11-21 17:01:48.273 test[48840:1303] 3*/

How to assign integer from NSMutableArray

I would like to assign NSInteger by using NSMutableArray is there any way to solve this?
It is not working on simulator and cut off when run the application.
NSInteger Section;
NSMutableArray dataSourceSection;
Section = (NSInteger)[dataSourceSection objectAtIndex:2];
Thank you.
A NSMutableArray only stores objects. NSInteger is not an object, but a primitive data type. There is a class NSNumber, however, that can be used instead to store numeric values inside objects. Here's one example.
NSNumber *five = [NSNumber numberWithInteger:5];
NSMutableArray *numbers = [NSMutableArray array];
[numbers addObject:five];
To get the object back and retrieve the integer value use,
NSNumber *firstNumber = [numbers objectAtIndex:0];
NSInteger valueOfFirstNumber = [firstNumber integerValue];
you can't pull an NSInteger out of an NSMutableArray, basically because you can't put anything rather than objects in. in your case NSNumber would be the way to put numbers in NSMutablearray. if you do so you can easily get hold of your object, which is an NSNumber, and convert it to a NSInteger by:
NSMutableArray *array = [[NSMutableArray alloc] init];
// populate the array with NSNumbers
NSInteger number = [[array objectAtIndex:2] intValue];

NSArray filled with bool

I am trying to create an NSArray of bool values. How many I do this please?
NSArray *array = [[NSArray alloc] init];
array[0] = YES;
this does not work for me.
Thanks
NSArrays are not c-arrays. You cant access the values of an NSArray with array[foo];
But you can use c type arrays inside objective-C without problems.
The Objective-C approach would be:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithBool:YES]];
//or
[array addObject:#(NO)];
...
BOOL b = [[array objectAtIndex:0] boolValue];
....
[array release];
EDIT: New versions of clang, the now standard compiler for objective-c, understand Object subscripting. When you use a new version of clang you will be able to use array[0] = #YES
Seems like you've confused c array with objc NSArray. NSArray is more like a list in Java, into which you can add objects, but not values like NSInteger, BOOL, double etc. If you wish to store such values in an NSArray, you first need to create a mutable array:
NSMutableArray* array = [[NSMutableArray alloc] init];
And then add proper object to it (in this case we'll use NSNumber to store your BOOL value):
[array addObject:[NSNumber numberWithBool:yourBoolValue]];
And that's pretty much it! If you wish to access the bool value, just call:
BOOL yourBoolValue = [[array objectAtIndex:0] boolValue];
Cheers,
Pawel
Use [NSNumber numberWithBool: YES] to get an object you can put in the collection.

Can a function return an object? Objective-C and NSMutableArray

I have an NSMutableArray. It's members eventually become members of an array instance in a class. I want to put the instantiantion of NSMutable into a function and to return an array object. If I can do this, I can make some of my code easier to read. Is this possible?
Here is what I am trying to figure out.
//Definition:
function Objects (float a, float b) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:a]];
[array addObject:[NSNumber numberWithFloat:b]];
//[release array]; ????????
return array;
}
//Declaration:
Math *operator = [[Math alloc] init];
[operator findSum:Objects(20.0,30.0)];
My code compiles if I instantiate NSMutableArray right before I send the message to the receiver. I know I can have an array argument along with the method. What I have problem seeing is how to use a function and to replace the argument with a function call. Any help is appreciated. I am interested in the concept not in suggestions to replace the findSum method.
Use autorelease to return objects you create in methods/functions.
NSMutableArray* Objects(float a, float b) {
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
// or: [NSMutableArray array];
[array addObject:[NSNumber numberWithFloat:a]];
[array addObject:[NSNumber numberWithFloat:b]];
return array;
}
Or simply:
NSMutableArray* Objects(float a, float b) {
return [NSMutableArray arrayWithObjects:
[NSNumber numberWithFloat:a],
[NSNumber numberWithFloat:b],
nil];
}