How to slice NSMutableArray [duplicate] - objective-c

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

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.

How do I query an NSArray when I know the attribute name and value? [duplicate]

This question already has answers here:
How to quickly search an array of objects in Objective-C
(4 answers)
NSPredicate for property of object in NSArray of NSArray
(1 answer)
Iterate through custom objects in a UIView and find matching properties
(2 answers)
Closed 5 years ago.
I am having a hard time getting the correct query type in my NSArray search, I tried the following which failed because its not searching for the correct value
BOOL isCashTender = [_tenderRows containsObject: #"Cash"];
I want to search inside the NSArray where there is a type attribute that contains the value Cash
I don't care how many rows/objects are inside the array I simply need to know if there is at least one type attribute containing the value Cash in there

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.

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