nsarray

Alphabetical sections in table table view in swift

放肆的年华 提交于 2019-11-28 09:06:28
I have a list of names sorted alphabetically, and now I want display these names in a table view. I'm struggling with grouping these names for each letter. My code looks like this: let sections:Array<AnyObject> = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] var usernames = [String]() func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cellID = "cell" let cell: UITableViewCell = self.tv.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell cell.textLabel?.text = usernames

Filter Array with dictionaries using NSPredicate

左心房为你撑大大i 提交于 2019-11-28 09:00:52
There is an Array with each element being a NSDictionary . NSMutableArray *mutArr = [NSMutableArray array]; for (Person *person in persons) { NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:person.name, @"name", person.email, @"email", nil]; [mutArr addObject:dict]; } self.arr = [[NSArray alloc] initWithArray:mutArr]; How to filer the arr with name or email contains string @"filter string" by using filterArrayUsingPredicate: method. Thanks in advance! Nit Please see the below example: NSArray *array = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"filter

Pick random element of NSArray in Objective-C [duplicate]

走远了吗. 提交于 2019-11-28 08:38:23
Possible Duplicate: Picking a Random Object in an NSArray I have have an array in Objective-C with strings: NSArray *tips; tips = [NSArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil]; I want a method that takes a random item from the array, and returns it. Is there a method, or how can I write one myself? Thanks. Use this code: uint32_t rnd = arc4random_uniform([tips count]); NSString *randomObject = [tips objectAtIndex:rnd]; EDIT: While working on my project i decided to create a category for NSArray. It's very simple but i found it useful. Here are the files: NSArray+Random.h #import

Encode NSArray or NSDictionary using NSCoder

断了今生、忘了曾经 提交于 2019-11-28 08:19:52
I was wondering whether or not it is possible to use the NSCoder method: - (void)encodeObject:(id)objv forKey:(NSString *)key to encode either an instance of NSArray or NSDictionary. If not how do you go about encoding them? I am trying to use NSKeyedArchived / NSKeyedUnarchiver to send data between phones using GameKit. I tried it and cannot seem to retrieve the string I stored in my array. The packet class I made comes through along with the packet type integer, but not the data in the array it seems. Thanks in advance! NSKeyedArchiver/Unarchiver should encode and decode NSArrays and

Sort NSArray using sortedArrayUsingFunction

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 08:08:26
问题 Any good example of sorting a NSArray using sortedArrayUsingFunction ? TY! 回答1: NSArray *sorted_bookings = [myUnsortedArray sortedArrayUsingFunction:Sort_Bookingdate_Comparer context:self]; NSInteger Sort_Bookingdate_Comparer(id id1, id id2, void *context) { // Sort Function Booking* booking1 = (Booking*)id1; Booking* booking2 = (Booking*)id2; return ([booking1.BOOKING_DATE compare:booking2.BOOKING_DATE]); } This I used to sort bookings by bookingdate. Booking is a class with a synthesized

how can I convert string to an array with separator?

拜拜、爱过 提交于 2019-11-28 07:53:07
I have a string in the following format myString = "cat+dog+cow" I need to store each string separated by + in to a array. Eg: myArray[0] = cat myArray[1] = dog myArray[2] = cow Can anyone tell me the proper way to do this? componentsSeparatedByString: splits the string and return the result in an array. NSArray *myArray = [myString componentsSeparatedByString:@"+"]; [myArray objectAtIndex:0];//cat [myArray objectAtIndex:1];//dog [myArray objectAtIndex:2];//cow Try this.. NSArray *arr = [myString componentsSeparatedByString:@"-"]; [arr objectAtIndex:0];//Hai [arr objectAtIndex:1];//Welcome It

Shuffling an array in objective-c [duplicate]

不羁的心 提交于 2019-11-28 07:44:25
Possible Duplicate: What’s the Best Way to Shuffle an NSMutableArray? I develop apps for iphone/iPad.I want to shuffle the objects stored in an NSArray.Is there any way to achieve it with objective-c? Saurabh Add a category to NSMutableArray, with code provided by Kristopher Johnson - // NSMutableArray_Shuffling.h #if TARGET_OS_IPHONE #import <UIKit/UIKit.h> #else #include <Cocoa/Cocoa.h> #endif // This category enhances NSMutableArray by providing // methods to randomly shuffle the elements. @interface NSMutableArray (Shuffling) - (void)shuffle; @end // NSMutableArray_Shuffling.m #import

Objective C: how to check if variable is NSArray or NSMutableArray

我怕爱的太早我们不能终老 提交于 2019-11-28 06:52:34
How can i check if a variable is an NSArray or an NSMutableArray? *Note that the implementation of NS{,Mutable}Array has changed since this answer was written. As a result, isKindOfClass: now works. On what platforms where and when, I don't know. Until it is documented as safe to do so, I would strongly recommend NOT writing code that tries to detect whether a collection is mutable or immutable. Even if it were safe, such a design pattern is almost always indicative of a serious design flaw. (For those with access) Filed rdar://10355515 asking for clarification. Consider: int main (int argc,

How to get the last object of an NSArray

◇◆丶佛笑我妖孽 提交于 2019-11-28 06:43:43
So I have an array which the amount of items could vary. I was wondering if there is anyway I can get the last object of an NSArray ? I did think of having like an int counter to trace the amount of items in the array however that seems too much of a hassle. Does anyone know anyway that's better than this? Expanding a bit on the question, there are several situations in which one could need the last object of an array, with different ways to obtain it. In a straight Cocoa program If one just wants to get the object in course of a standard Cocoa program, then this will do it: [myArray

Sorting a NSArray

戏子无情 提交于 2019-11-28 06:07:17
I have a NSArray of objects. Those objects have an int attribute called "distance". I would like to sort my array by distance. Could someone please tell me how to do that ? I can't understand how the sortUsingSelector or sortUsingDescriptor methods are working... Thanks I assume you're using an NSMutableArray, because an NSArray is immutable. When you sort something, you want the end result to be arranged as x1 <= x2 <= x3 <= x4 <= ... <= xN How to define this <= ? There are 3 solutions in ObjC. 1. -sortUsingSelector: [arr sortUsingSelector:@selector(foo:)] means that its elements will be