nsarray

How to let the sortedArrayUsingSelector using integer to sort instead of String?

霸气de小男生 提交于 2019-11-27 09:44:30
I have some data like this : 1, 111, 2, 333, 45, 67, 322, 4445 NSArray *array = [[myData allKeys]sortedArrayUsingSelector: @selector(compare:)]; If I run this code, it sorted like this: 1, 111, 2,322, 333, 4445, 45, 67, but I actually want this: 1, 2, 45, 67, 111, 322, 333, 4445 How can I implement it? thz u. Expanding on Paul Lynch's answer, here's an example I have doing exactly this using a comparison method as a category on NSString . This code handles only the case of numbers followed by optional non-numeric qualifiers, but you could extend it to handle cases like "1a10" etc. if desired.

Compare two arrays with the same value but with a different order

﹥>﹥吖頭↗ 提交于 2019-11-27 09:19:06
I have 2 nsarray, with the same values but in different order. NSArray * array1 = {0,1,2,3} NSArray * array2 = {2,3,1,0} I need a method to determinate if two arrays have the same values in a different order. Kind of -(BOOL) isSameValues:(NSArray*)array1 and:(NSArray*)array2; if ([[NSSet setWithArray:array1] isEqualToSet:[NSSet setWithArray:array2]]) { // the objects are the same } You can use NSCountedSet for that purpose: - (BOOL)isSameValues:(NSArray*)array1 and:(NSArray*)array2 { NSCountedSet *set1 = [NSCountedSet setWithArray:array1]; NSCountedSet *set2 = [NSCountedSet setWithArray:array2

Sort NSArray of custom objects based on sorting of another NSArray of strings

倖福魔咒の 提交于 2019-11-27 09:18:10
I have two NSArray objects that I would like to be sorted the same. One contains NSString objects, the other custom Attribute objects. Here is what my "key" NSArray looks like: // The master order NSArray *stringOrder = [NSArray arrayWithObjects:@"12", @"10", @"2", nil]; The NSArray with custom objects: // The array of custom Attribute objects that I want sorted by the stringOrder array NSMutableArray *items = [[NSMutableArray alloc] init]; Attribute *attribute = nil; attribute = [[Attribute alloc] init]; attribute.assetID = @"10"; [items addObject:attribute]; attribute = [[Attribute alloc]

Plist Array to NSDictionary

懵懂的女人 提交于 2019-11-27 08:42:59
I have a plist: <plist version="1.0"> <array> <dict> <key>name</key> <string>Alabama</string> <key>abreviation</key> <string>AL</string> <key>date</key> <string>1819</string> <key>population</key> <string>4,627,851</string> <key>capital</key> <string>Montgomery</string> <key>largestCity</key> <string>Birmingham</string> </dict> <dict> <key>name</key> <string>Alaska</string> <key>abreviation</key> <string>AK</string> <key>date</key> <string>1959</string> <key>population</key> <string>683,478</string> <key>capital</key> <string>Juneau</string> <key>largestCity</key> <string>Anchorage</string> <

Putting JSON into an Array

夙愿已清 提交于 2019-11-27 08:40:51
问题 I'm a noob when it comes to requests and JSON. Inside my app I send to the server and get back stuff so I can use it of course. I tried looking up different things but none really seem to be what I'm looking for. So I'm getting back what seems to be formatted JSON. What I want to know how to do is put it into a NSMutable array. The way I get this JSON is by using AFNetworking's AFJSONRequestOperation. My response looks like this. { id = 38; name = "St. Martin Hall"; }, { id = 40; name =

Filter NSArray of custom objects

不打扰是莪最后的温柔 提交于 2019-11-27 08:37:37
问题 I have a NSArray of Contact objects, we can call it contacts . Contact is a superclass, FacebookGroup and Individual are subclasses of Contact . FacebookGroup has a property called individuals which is a set of Individual objects. I also have a NSArray of NSString objects, we can call it userIDs . What I want to do is create a new NSArray from the existing contacts array that match the userIDs in userIDs . So if contacts has 3 Contact objects with userID 1,2 and 3. And my userIDs has a

how to resolve the issue: “'Instance method '-arrayByPerformingSelector:' not found (return type defaults to 'id')”

拥有回忆 提交于 2019-11-27 08:31:14
问题 i saw in one post -(id)arrayByPerformingSelector declaration in interface should do , but when i tried it this declaration was treated as separate method and incomplete implementation issue came... sorry this is quite a silly doubt i am asking but I'm a newbie to iOS and wasn't able to find out whats wrong with this.. self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]]; self.segmentedControl.segmentedControlStyle =

Need to convert NSData to NSArray

大城市里の小女人 提交于 2019-11-27 08:15:26
问题 I am pulling down a plist from a URL using an NSURLConnection, which returns an NSData object. I would like to convert this to an NSArray. Can anyone help me? I am refactoring a method that currently does a synchronous request to the URL, using arrayWithContentsOfURL: to get the NSArray. Thanks!! 回答1: Use +[NSPropertyListSerialization dataWithPropertyList:format:options:error:] to convert the data, then check if the result -isKindOfClass:[NSArray class] . 回答2: Use NSKeyedArchiver for

Sorting NSArray containing NSDate objects

旧街凉风 提交于 2019-11-27 08:00:42
问题 I have an array containing NSDate objects. With Objective-C, I am able to sort this array by using: NSArray *array = [unsortedArray sortedArrayUsingSelector:@selector(compare:)] I'm wondering if there's a Swift equivalent of doing this same thing. Relatively new to Swift here, hacking my way through. 回答1: Using the native Array type in Swift. If you are interfacing with legacy code from ObjC, use this: let array = unsortedArray.sortedArrayUsingSelector("compare:") 回答2: If you use Swift array.

How to display unique elements of an array using Swift? [duplicate]

末鹿安然 提交于 2019-11-27 07:47:49
问题 This question already has an answer here: Removing duplicate elements from an array in Swift 41 answers Swift 3 Generics: How to Find The Common Set of Two Generic Arrays 2 answers I have a formula retuning an array as example var Array = [a,s,d,s,f,g,g,h,e] . What I want is to run a for loop or some other option that gives me back a,s,d,f,g,h,e - only the unique values. How can I do this with ios Swift? 回答1: If you don't care about order: Simply use a set: let set: Set = ["a", "s", "d", "s",