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

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.

Related

How to modify value in NSArray object? [duplicate]

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].

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.

Sort an array of NSDictionary based on dates [duplicate]

This question already has answers here:
How can I sort an NSArray containing NSDictionaries?
(3 answers)
Closed 10 years ago.
I'm looking for a way to sort an NSMutableArray of dictionaries based on dates contained in the dictionaries. Each one looks like this:
{
name: tom,
surname: smith,
date of birth (NSDate): 2013-02-21 15:25:27
}
I know of the methods earlierDate etc., but since they only compare two dates I'm just a bit stuck on how to iterate through the array so that I come out with a fully ordered array of dictionaries?
using sortUsingComparator this can be solved easily:
[yourArray sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
return [a[#"dateOfBirth"] compare:b[#"dateOfBirth"]];
}];

Sorting an array in objective-c [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sorting in nsmutable array
I have 2 NSMutableArray array. I have to sort it. Is there any function defined in Objective-c for direct sort.
like
NSMutableArray *sortedarray_variable= (sorting method) array_variable;
Where sorting method returns sorted array.
Of course. NSArray provides several methods

How to slice NSMutableArray [duplicate]

This question already has an answer here:
How to slice an NSArray in Objective-C?
(1 answer)
Closed 8 years ago.
I have a NSMutableArray and need objects [0:5] only. Is there a simple way to slice? Can I drop all objects after index? Can I copy a sub-array to another NSMutableArray?
Use the instance method - (NSArray *)subarrayWithRange:(NSRange)range.
For example:
NSArray* slicedArray = [wholeArray subarrayWithRange:NSMakeRange(0, 5)];
I see James Bedford already answered how to extract a range of indexes. To delete the objects in a range from an NSMutableArray, you can use [wholeArray removeObjectsInRange:...]. To delete all objects after a particular index, you can create an appropriate range as NSMakeRange(index, wholeArray.count - index).