I only have this code in m. file
NSMutableArray * arrayOfBools;
arrayOfBools=[[NSMutableArray alloc] initWithCapacity:1000];
NSNumber *ijk =(NSNumber*) 9;
[arrayOfBools addObject:ijk];
Get error o this [arrayOfBools addObject:ijk];
You can't declare and set an NSNumber like this: NSNumber *ijk =(NSNumber*) 9;.
This will set it to an integer (9).
Use this:
NSNumber *ijk = [NSNumber numberWithInt:9];
The third line, the declaration of the NSNumber is incorrect. if you are attempting to wrap a bool into a NSNumber, use NSNumber *test = [NSNumber numberWithBool:YES];
Related
This must be quite basic, but I was wondering how to add an integer to an array?
I know I can add strings like this:
NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:#"0"];
[trArray addObject:#"1"];
[trArray addObject:#"2"];
[trArray addObject:#"3"];
But I guess I can't simply add integers like so:
NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:0];
[trArray addObject:1];
[trArray addObject:2];
[trArray addObject:3];
At least the compiler isn't happy with that and tells me that I'm doing a cast without having told it so.
Any explanations would be very much appreciated.
Yes that's right. The compiler won't accept your code like this. The difference is the following:
If you write #"a String", it's the same as if you created a string and autoreleased it. So you create an object by using #"a String".
But an array can only store objects (more precise: pointers to object). So you have to create objects which store your integer.
NSNumber *anumber = [NSNumber numberWithInteger:4];
[yourArray addObject:anumber];
To retrive the integer again, do it like this
NSNumber anumber = [yourArray objectAtIndex:6];
int yourInteger = [anumber intValue];
I hope my answer helps you to understand why it doesn't work. You can't cast an integer to a pointer. And that is the warning you get from Xcode.
EDIT:
It is now also possible to write the following
[yourArray addObject:#3];
which is a shortcut to create a NSNumber. The same syntax is available for arrays
#[#1, #2];
will give you an NSArray containing 2 NSNumber objects with the values 1 and 2.
You have to use NSNumbers I think, try adding these objects to your array: [NSNumber numberWithInteger:myInt];
NSMutableArray *trArray = [[NSMutableArray alloc] init];
NSNumber *yourNumber = [[NSNumber alloc] numberWithInt:5];
[trArray addObject: yourNumber];
You can also use this if you want to use strings:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat:#"%d",1]];
[[array objectAtIndex:0] intValue];
i have an array of 5 objects.
i want to assign object which is at index 1, to an NSSTRING.
nsstring *abc = [array objectAtindex:1];
i know this is wrong syntax, this is returning object , something like this.
how can i get value which is at index 1 and assign it to an string?
regards
Erm.. this is the correct syntax :)
Apart the name of the string class:
NSString *abc = [array objectAtIndex:1];
mind that this won't create a copy of the string, if you need to copy it use
NSString *abc = [NSString stringWithString:[array objectAtIndex:1]];
As Eiko notes you can directly copy the string object if you need to:
NSString *abc = [[array objectAtIndex:1] copy];
Arrays are zero based in Objective-C land. So...
NSArray *array = [NSArray arrayWithObjects:#"one", #"two", nil];
NSString *abc = [array objectAtIndex:1];
Would return the second object in the array. Zero would return the first.
I have a small problem with iPhone SDK. I have 4 values in my xib, 4 UITextFields where I insert hostname, description, name and PORT.
The Port is an NSNumber. I must convert text (UITextField) into NSNumber but I don't know how.
I tried the following:
NSNumber *temp = [[NSNumber alloc] initWithString:portTextField.text];
serverObj.port = temp;
but my app crashes. I don't have problems with NSString (description ecc.)
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:textField.text];
[f release];
And that should get the job done.
Just use the 'integerValue' method of NSString, to get a 'NSInteger'. Then you can create a NSNumber object from it.
serverObj.port = [ NSNumber numberWithInteger: [ portTextField.text integerValue ] ];
There is no method for NSNumber objects called initWithString: (that's why your app crashes). You need to use NSNumberFormatter's method numberFromString:
My problem is since an enum in objective-c essentially is an int value, I am not able to store it in a NSMutableArray. Apparently NSMutableArray won't take any c-data types like an int.
Is there any common way to achieve this ?
typedef enum
{
green,
blue,
red
} MyColors;
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:
green,
blue,
red,
nil];
//Get enum value back out
MyColors greenColor = [list objectAtIndex:0];
Wrap the enum value in an NSNumber before putting it in the array:
NSNumber *greenColor = [NSNumber numberWithInt:green];
NSNumber *redColor = [NSNumber numberWithInt:red];
NSNumber *blueColor = [NSNumber numberWithInt:blue];
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:
greenColor,
blueColor,
redColor,
nil];
And retrieve it like this:
MyColors theGreenColor = [[list objectAtIndex:0] intValue];
A modern answer might look like:
NSMutableArray *list =
[NSMutableArray arrayWithArray:#[#(green), #(red), #(blue)]];
and:
MyColors theGreenColor = ((NSInteger*)list[0]).intValue;
Macatomy's answer is correct. But instead of NSNumber I would suggest you use NSValue. That is its purpose in life.
NSMutableArray *corners = [[NSMutableArray alloc] initWithObjects:
#(Right),
#(Top),
#(Left),
#(Bottom), nil];
Corner cornerType = [corner[0] intValue];
You can wrap your enum values in a NSNumber object:
[NSNumber numberWithInt:green];
To go with NSNumber should be the right way normally. In some cases it can be useful to use them as NSString so in this case you could use this line of code:
[#(MyEnum) stringValue];
So I'm a Flash guy and I'm trying to convert the following code to Object C:
var slot:Object = new Object();
slot.id = i;
slot.xPos = 25*i;
slot.yPos = 25*i;
slot.isEmpty = False;
// push object to array
arrGrid.push(slot);
Later I can override like:
arrGrid[0].isEmpty = True;
I can't seem to find a reference to creating generic objects in Object C. Can someone help?
Well assuming you are doing something with the iphone or mac in cocoa you can simply subclass NSObject (the base class in objective-c).
You need a .h and .m so for you example it would be something like:
(Note that I used slotId instead of id because id is a keyword in objective-c)
Slot.h
// Slot.h
#interface Slot : NSObject {
NSInteger slotId;
float xPos;
float yPos;
BOOL empty;
}
#property NSInteger slotId;
#property float xPos;
#property float yPos;
#property BOOL empty;
#end
// Slot.m
#import "Slot.h"
#implementation Slot
#synthesize slotId;
#synthesize xPos;
#synthesize yPos;
#synthesize empty;
#end
That defines a simple Slot object with 4 properties which can be accessed using dot notation such as:
s = [[Slot alloc] init];
s.empty = YES;
s.xPos = 1.0;
s.yPos = 1.0;
There are a lot of variations for which data types you use and how you define the properties, etc depending on what kind of data you are dealing with.
If you wanted to add a slot object to an array one simple example:
// create an array and add a slot object
NSMutableArray *arr = [NSMutableArray array];
Slot *slot = [[Slot alloc] init];
[arr addObject:slot];
// set the slot to empty
[[arr objectAtIndex:0] setEmpty:YES];
If you're only using this Object instance to store named values, you can use an instance of NSMutableDictionary instead, although you'll need to wrap your integer values with NSNumber instances:
NSMutableDictionary * obj = [NSMutableDictionary dictionary];
[obj setObject: [NSNumber numberWithInt: i] forKey: #"id"];
[obj setObject: [NSNumber numberWithInt: i*25] forKey: #"xPos"];
[obj setObject: [NSNumber numberWithInt: i*25] forKey: #"yPos"];
[obj setObject: [NSNumber numberWithBool: NO] forKey: #"isEmpty"];
Then you'd add these to an NSMutableArray allocated using [NSMutableArray array] or similar:
[array addObject: obj];
To get the integer/boolean values out of the dictionary, you'd do the following:
int i = [[obj objectForKey: #"id"] intValue];
BOOL isEmpty = [[obj objectForKey: #"isEmpty"] boolValue];