What does #[] stand for in objective c? - objective-c

I've come across the #[] in the following context
self.searches = [#[] mutableCopy];
What is it?

self.searches = [[NSArray array] mutableCopy];

It's an array literal. In another words: [NSArray arrayWithObjects:nil] in short form.
You can add objects in it like this
NSArray *array = #[#"one", #"two"];

Related

Objective-c - Sort some Numbers stored is variables

hello
I want to sort(ascending) some numbers stored in different variables(like int a=50,int b=60, etc..)!
How can i do that?
Thanks for any help!
You need to store the numbers in an NSArray, packaged in NSNumbers. Then you can do normal array sorting:
NSArray *array = [NSArray arrayWithObjects:
[NSNumber numberWithInt:a],
[NSNumber numberWithInt:b],
nil
];
NSArray *sorted = [array sortedArrayUsingSelector:#selector(compare:)];
Put them into NSArray or NSMutableArray and sort the array

Add NSStrings to mutable array

I created a mutable array and I have two NSString variables. Now I want to add these two NSStrings to my array. How is this possible? Thanks.
Use the addObject function of you NSMutableArray.
eg.
[myNSMutableArray addObject:myString1];
[myNSMutableArray addObject:myString2];
Jhaliya's answer is correct. +1 vote.
I added a immutable version so you can see the difference. If you dont want to remove or add more objects (NSStrings) to your container, I would recommend using an Immutable version.
Mutable version:
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
NSString *string_one = #"One"];
[mutableArray addObject:string_one];
//Or
[mutableArray addObject:#"Two"];
NSLog(#"%#", mutableArray);
Immutable version
NSArray *immutableArray = [NSArray arrayWithObjects:#"One", #"Two", nil];
NSLog(#"%#", immutableArray);
You can add at NSMutableArray allocation.
Like :
NSMutableArray *test = [NSMutableArray arrayWithObjects:#"test1",#"test2",nil];

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.

How to return an NSMutableArray from an NSSet

I'm able to put the contents of an NSSet into an NSMutableArray like this:
NSMutableArray *array = [set allObjects];
The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray. How should this be fixed?
Since -allObjects returns an array, you can create a mutable version with:
NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];
Or, alternatively, if you want to handle the object ownership:
NSMutableArray *array = [[set allObjects] mutableCopy];
I resolved crashing by using NSMutableArray's method 'addObjectsFromArray' to assign all NSSet objects to NSMutableArray like:
[mutableArray addObjectsFromArray:[cg_Schedule.schedule_Days allObjects]];
Hope this will helps you.
For an ordered set use:
NSArray *myArray = [[myOrderedSet array] mutableCopy];

How to declare a two dimensional array of string type in Objective-C?

How do I declare a two dimensional array of string type in Objective-C?
First, you might consider using a class to hold your inner array's strings, or loading it from a plist file (in which it is easy to make an 2d array of strings).
For direct declarations, you have a couple of options. If you want to use an NSArray, you'll have to manually create the structure like this:
NSMutableArray *strings = [NSMutableArray array];
for(int i = 0; i < DESIRED_MAJOR_SIZE; i++)
{
[strings addObject: [NSMutableArray arrayWithObject:#"" count:DESIRED_MINOR_SIZE]];
}
Or, using array literals, you can get an immutable version like this:
NSArray *strings = #[ #[ #"A", #"B", #"C" ], #[ #"D", #"E", #"F" ], #[ #"G", #"H", #"I" ] ]
You can then use it like this:
NSString *s = [[strings objectAtIndex:i] objectAtIndex:j];
This is somewhat awkward to initialize, but it is the way to go if you want to use the NSArray methods.
An alternative is to use C arrays:
NSString *strings[MAJOR_SIZE][MINOR_SIZE] = {0}; // all start as nil
And then use it like this:
NSString *s = strings[i][j];
This is less awkward, but you have to be careful to retain/copy and release values as you put them in to and remove them from the array. (Unless you're using ARC, of course!) NSArray would do this for you but with C-style arrays, you need to do something like this to replace an array:
[strings[i][j] release];
strings[i][j] = [newString retain];
The other difference is that you can put nil in the C-style array, but not the NSArrays - you need to use NSNull for that. Also take a look at Stack Overflow question Cocoa: Memory management with NSString for more about NSString memory management.
If you want to declare and initialize a two-dimensional array of strings, you can do this:
NSArray *myArray = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:#"item 1-1", #"item 1-2", nil],
[NSArray arrayWithObjects:#"item 2-1", #"item 2-2", nil],
[NSArray arrayWithObjects:#"item 3-1", #"item 3-2", nil],
[NSArray arrayWithObjects:#"item 4-1", #"item 4-2", nil],
nil];
This has the benefit of giving you an immutable array.
I might be self-advertising but I wrote a wrapper over NSMutableArray fore easy use as a 2D array. It available on GitHub as CRL2DArray here. https://github.com/tGilani/CRL2DArray