NSArray or NSMutableArray for NSNumber Listing - objective-c

Okay, so I want to do something like this:
int array[] = {1, 2, 3, 4, 5};
How would I go about that if I want to use NSArray and NSNumbers instead of ints?
**Note:
I do not want something like
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: ];
I need to be able to put them all in a single set separated by a comma. (Weird but it makes my program easier to handle in that format. I'm mimicking some java code where that is done a lot, so itl make it easier to follow the tutorial.)

If you want to add all the elements at once, and you're not going to change it, then us an NSArray. You can fill it like this:
NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],nil];

NSArray *array = #[#1,#2,#3,#4];

you could either do this
NSArray* array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4], nil];

Related

Sort NSMutableArray based on strings from another NSArray

I have an NSArray of strings that I want to use as my sort order:
NSArray *permissionTypes = [NSArray arrayWithObjects:#"Read", #"Write", #"Admin", nil];
I then have a NSMutableArray that may or may not have all three of those permissions types, but sometimes it will only be 2, sometimes 1, but I still want it sorted based on my permissionsTypes array.
NSMutableArray *order = [NSMutableArray arrayWithArray:[permissions allKeys]];
How can I always sort my order array correctly based on my using the permissionTypes array as a key?
I would go about this by creating a struct or an object to hold the permission types.
Then you can have...
PermissionType
--------------
Name: Read
Order: 1
PermissionType
--------------
Name: Write
Order: 2
and so on.
Then you only need the actual array of these objects and you can sort by the order value.
[array sortUsingComparator:^NSComparisonResult(PermissionType *obj1, PermissionType *obj2) {
return [obj1.order compare:obj2.order];
}];
This will order the array by the order field.
NSMutableArray *sortDescriptors = [NSMutableArray array];
for (NSString *type in permissionTypes) {
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:type ascending:YES] autorelease];
[sortDescriptors addObject:descriptor];
}
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
Use whichever sorting method on NSMutableArray you prefer, you will either provide a block or a selector to use for comparing two elements. In that block/selector rather than comparing the two strings passed in directly look each up in your permissionTypes array using indexOfObject: and compare the resulting index values returned.
I suggest you another approuch:
- (void)viewDidLoad
{
[super viewDidLoad];
arrayPermissions = [[NSMutableArray alloc] init];
NSDictionary *dicRead = [NSDictionary dictionaryWithObjectsAndKeys:
#"Read", #"Permission", nil];
NSDictionary *dicWrite = [NSDictionary dictionaryWithObjectsAndKeys:
#"Write", #"Permission", nil];
NSDictionary *dicAdmin = [NSDictionary dictionaryWithObjectsAndKeys:
#"Admin", #"Permission", nil];
NSLog(#"my dicRead = %#", dicRead);
NSLog(#"my dicWrite = %#", dicWrite);
NSLog(#"my dicAdmin = %#", dicAdmin);
[arrayPermissions addObject:dicRead];
[arrayPermissions addObject:dicWrite];
[arrayPermissions addObject:dicAdmin];
NSLog(#"arrayPermissions is: %#", arrayPermissions);
// create a temporary Dict again
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys: arrayPermissions, #"Permission", nil];
// declare one dictionary in header class for global use and called "filteredDict"
self.filteredDict = temp;
self.sortedKeys =[[self.filteredDict allKeys]
sortedArrayUsingSelector:#selector(compare:)];
NSLog(#"sortedKeys is: %i", sortedKeys.count);
NSLog(#"sortedKeys is: %#", sortedKeys);
}
hope help

What does #[] stand for in 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"];

How can I retrieve all the contents of an NSDictionary?

I want to select and retrieve all the contents from an NSDictionary. I have a structure like this
- (void)viewDidLoad {
[super viewDidLoad];
listaOggetti = [[NSMutableArray alloc] init];
NSArray *arrayOne = [NSArray arrayWithObjects: #"First",#"Second",#"Third", nil];
NSArray *sortedOne = [arrayOne sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictOne = [NSDictionary dictionaryWithObject:sortedOne forKey:#"Elementi"];
NSArray *arrayTWo = [NSArray arrayWithObjects:#"First1",#"Second1" ..., nil];
NSArray *sortedTwo = [arrayTwo sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictTwo = [NSDictionary dictionaryWithObject:sortedTWo forKey:#"Elementi"];
NSArray *arrayThree = [NSArray arrayWithObjects:#"First2",#"Second2" ... , nil];
NSArray *sortedThree = [arrayThree sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictThree = [NSDictionary dictionaryWithObject:sortedThree forKey:#"Elementi"];
[listaOggetti addObject:dictOne];
[listaOggetti addObject:dictTwo];
[listaOggetti addObject:dictThree];
}
And I want to retrieve all the objects for the key #"Elementi" (should be around 45) in order to add them in another array, like:
NSDictionary *dict = [listaOggetti objectAtIndex:indexPath.section];
NSArray *array = [dict objectForKey:#"Elementi"];
cellValue = [array objectAtIndex:indexPath.row] ;
(With this, dict is only 9 objects filled in my project).
At the end, the *array should be around 45 objects filled. I tried with allValues, but didn't work.
How can I fix it?
The easiest is to do this in -viewDidLoad:
NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[allObjects addObjectsFromArray:sortedOne];
[allObjects addObjectsFromArray:sortedTwo];
[allObjects addObjectsFromArray:sortedThree];
Alternately, you can get them from the dictionaries in a similar fashion:
NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:0] objectForKey#"Elementi"];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:1] objectForKey#"Elementi"];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:2] objectForKey#"Elementi"];
What you are failing to understand is that listaOggetti is an NSMutableArray containing three objects. When you call
NSDictionary *dict = [listaOggetti objectAtIndex:indexPath.section];
the result is that dict is a single dictionary, one of the three objects in listaOggetti. Therefore when you call
NSArray *array = [dict objectForKey:#"Elementi"];
the result is that array is the object for the key #"Elementi" of that one single dictionary dict. Your code makes no attempt to combine the three DIFFERENT dictionaries or to combine the three arrays, each set as objectForKey:#"Elementi" for the three DIFFERENT dictionaries.
If you want one array that is the concatenation of all three different arrays, then use one of the snippets provided above. In both of these snippets, the result is that allObjects is an NSMutableArray containing all three arrays, in order.

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

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