How to modify value in NSArray object? [duplicate] - objective-c

This question already has answers here:
How do you change the elements within an NSArray?
(3 answers)
Closed 7 years ago.
I want to modify value in NSArray at index 0.
Here is sample program
NSArray *test = #[#"abc",#"rec",#"myPM"];
NSLog (#"%#",[test objectAtIndex:0]);
test[0]=#"001";
NSLog (#"%#",[test objectAtIndex:0]);
Here I am getting lot of errors. please help me in this.

You should use an NSMutableArray
And with this method :
[yourArray replaceObjectAtIndex:INDEX withObject:OBJECT].

Related

Randomly select from an array of strings in Objective-C? [duplicate]

This question already has answers here:
Picking a Random Object in an NSArray
(8 answers)
Closed 3 years ago.
I have done some digging, and I don't believe this question has been answered yet (though I would imagine it would be simple).
I want to select a random string from an array. For example:
NSArray *strings = #[#"String1", #"String2", #"String3"];
NSString *randomString = // how to randomly select one of the three strings in the array?
You want arc4random_uniform():
NSString *randomString = strings[arc4random_uniform(strings.count)];
More info in this SO answer.

Searching an object (by an attribute) in a NSMutableArray [duplicate]

This question already has answers here:
Finding a particular instance in an array by a property
(5 answers)
Closed 8 years ago.
I need to get a better way to find an object in a NSMutableArray.
At the moment i do it this way:
for(classOfTheObject *thisItem in arrayOfObjects){
if(thisItem.foreign_key == serchThisObject.foreign_key){
// found it
}
}
but this is a very bad way i think. Is it possible to get the object without a for loop?
In an array it will always require some type of loop/enumaration to actually find it. If foreign_key is the only search/identification criteria that you use then you may consider using an NSDictionary with the value of foreign_key as key.
If I were you,I would use NSPredicate :
NSPredicate *applePred = [NSPredicate predicateWithFormat:
#"employer.name == 'Apple'"];
NSArray *appleEmployees = [people filteredArrayUsingPredicate:applePred];

Objective-C - sort array of strings based on string slice [duplicate]

This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
How to do a natural sort on an NSArray?
(5 answers)
Closed 9 years ago.
NSMutableArray looks like ("xxx_20", "xxx_18", "xxx_16", "xxx_19", "xxx_17")
I want to sort it into ("xxx_16", "xxx_17", "xxx_18", "xxx_19", "xxx_20")
I've tried to use selectors, but I can't even get proper syntax. Does anyone know how to do this properly?
Try this,
NSMutableArray *myarray = [ NSMutableArray arrayWithObjects:#"xxx_20", #"xxx_18", #"xxx_16", #"xxx_19", #"xxx_17", nil];
NSArray *sortedArray = [myarray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"%#",sortedArray);
It worked fine at my end.

NSString retain count in Objective-C [duplicate]

This question already has answers here:
When to use -retainCount?
(11 answers)
Closed 9 years ago.
NSString* nsString=[[NSString alloc]initWithString:#"nsString"];
NSLog(#"nsString RetainCount:%li",[nsString retainCount]);
the corresponding result is:
2013-03-04 11:18:03.291 ARC[655:303] nsString RetainCount:-1
in addition:
if use init a NSMutableString instance;
it return 1;
http://whentouseretaincount.com
Immutable NSStrings generated at compile time are singletons. Thus, they don't do the retain/release dance at all.
NSString detects when it is initialized with such and just returns the string directly. You'd find that the object returned by alloc in that code is different than the one returned by the init... call.

Order NSDictionary allObjects by Keys [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can i sort the NSDictionary on basis of key in Objective-C?
I have a several strings in a .plist file that I am trying to return into the NSDictionary then into an array but the order being returned isn't the same as I saved it in the playlist.
So far I have tried
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *strs = [NSArray arrayWithArray:[dict allKeys]];
strs = [strs sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
but that is only returning the key names.
I actually was just looking through a few forums on google and found that this worked: Can i sort the NSDictionary on basis of key in Objective-C?
No, you can't.
You could keep an ordered array with your keys around as well, though.