nsarray

What's the difference between a dictionary and an array?

混江龙づ霸主 提交于 2019-11-27 21:00:11
问题 What is the difference between a dictionary and an array, especially when working with PLIST files? What are the advantages of using one over the other? Thanks! 回答1: Both NSDictionary and NSArray are collection classes, i.e. the group together other objects. An NSArray is an 'ordered collection' - every item in the collection has an integer index, so there is an explicit order to the items. If you swap the order of items in the collection then the collection is no longer the 'same' as the

Sort NSArray with sortedArrayUsingComparator

巧了我就是萌 提交于 2019-11-27 20:29:58
问题 In Objective-C I can sort an NSArray using this statement: NSArray *sortedArray = [persons sortedArrayUsingComparator:^NSComparisonResult(Person *p1, Person *p2) { return [p1.name compare:p2.name]; }]; I'm unable to reproduce the same statement with Swift . All I found was using Array . 回答1: You can either use Swift's built in sort functions or, since a Swift array is bridged to NSArray you can call sortedArrayUsingComparator from swift directly. Using Swift's sorted function: var sortedArray

Converting an NSArray of Dictionaries to JSON array in iOS

时光怂恿深爱的人放手 提交于 2019-11-27 20:09:26
问题 I need to send an NSArray to the server in the JSON array format. How can I convert it to JSON . This is a sample of my NSArray that I have to pass. array([0] => array('latitude'=>'10.010490', 'longitude'=>'76.360779', 'altitude'=>'30.833334', 'timestamp'=>'11:17:23', 'speed'=>'0.00', 'distance'=>'0.00'); [1] => array('latitude'=>'10.010688', 'longitude'=>'76.361378', 'altitude'=>'28.546305', 'timestamp'=>'11:19:26', 'speed'=>'1.614', 'distance'=>'198.525711') )` and the required format is

Is it possible to filter an NSArray by class?

安稳与你 提交于 2019-11-27 20:06:26
Is there a way construct a predicate to filter by class type? I currently loop through the array and check to the class of each object. Maybe there is a cleaner way? You could add a category to NSObject that adds a "cf_className" method, like so: @interface NSObject (CFAdditions) - (NSString *) cf_className; @end @implementation NSObject (CFAdditions) - (NSString *) cf_className { return NSStringFromClass([self class]); } @end From there, you can use predicates like: NSPredicate * p = [NSPredicate predicateWithFormat:@"cf_className = %@", aClass]; NSArray * filtered = [anArray

Store NSArray In Core Data Sample Code?

半城伤御伤魂 提交于 2019-11-27 20:04:09
I have been searching for some sample code on how to store an NSArray in Core Data for awhile now, but haven't had any luck. Would anyone mind pointing me to some tutorial or example, or better yet write a simple sample as an answer to this question? I have read this but it doesn't show an example of how to go about implementing a transformable attribute that is an NSArray . Thanks in advance! Resh32 If you really need to do it, then encode as data. I simply created a new filed called receive as NSData (Binary data). Then in the NSManagedObject implementation: -(void)setReceiveList:(NSArray*

NSMutablearray move object from index to index

风流意气都作罢 提交于 2019-11-27 19:13:01
I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called? Another way to ask this is how to reorder an NSMutableArray? id object = [[[self.array objectAtIndex:index] retain] autorelease]; [self.array removeObjectAtIndex:index]; [self.array insertObject:object atIndex:newIndex]; That's all. Taking care of the retain count is important, since the array might be the only one referencing the object. ARC compliant category: NSMutableArray+Convenience.h @interface NSMutableArray

Key Value Observing with an NSArray

时光总嘲笑我的痴心妄想 提交于 2019-11-27 18:34:41
I've looked on SO for examples of using Key Value Observing with an NSArray (or NSMutableArray ) and apparently you need to use an NSArrayController (which unlike KVO I'm not familiar with), but I haven't found concrete examples of how to do this. Can anyone explain with some sample code? For instance, if I have a GameModel which represents its player names with an NSArray (playerNameArray) of NSStrings . I want to observe those strings (the view controller observes the model's data) to update various things in the view. How do I get notification that the player name array has changed? EDIT :

How to create a NSString from a format string like @“xxx=%@, yyy=%@” and a NSArray of objects?

五迷三道 提交于 2019-11-27 18:00:37
Is there any way to create a new NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects? In the NSSTring class there are many methods like: - (id)initWithFormat:(NSString *)format arguments:(va_list)argList - (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList + (id)stringWithFormat:(NSString *)format, ... but non of them takes a NSArray as an argument, and I cannot find a way to create a va_list from a NSArray... Peter N Lewis It is actually not hard to create a va_list from an NSArray. See Matt Gallagher's excellent article on the subject

I want to sort an array using NSSortDescriptor

荒凉一梦 提交于 2019-11-27 17:47:06
I am having a problem regarding sorting an array w.r.t database: NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; [mGlossaryArray sortUsingDescriptors:sortDescriptors]; [sorter release]; Here in database there are some first capital letters and because of that capital letter it does not show me proper sorted output. Here i am sorting an array with r.t "w" which is my table column in database. Here I have attach the screen shot for the output, which says that "Cancer" comes first than "c", but

JSON Parsing in iOS 7

岁酱吖の 提交于 2019-11-27 17:28:00
I am creating an app for as existing website. They currently has the JSON in the following format : [ { "id": "value", "array": "[{\"id\" : \"value\"} , {\"id\" : \"value\"}]" }, { "id": "value", "array": "[{\"id\" : \"value\"},{\"id\" : \"value\"}]" } ] which they parse after escaping the \ character using Javascript. My problem is when i parse it in iOS using the following command : NSArray *result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&localError]; and do this : NSArray *Array = [result valueForKey:@"array"]; Instead of an Array I got NSMutableString