nsarray

iPhone - getting unique values from NSArray object

和自甴很熟 提交于 2019-12-17 17:24:31
问题 I have an NSArray formed with objects of a custom class. The class has 3 (city, state, zip) string properties. I would like to get all unique state values from the array . I did read through the NSPredicate class but couldn't make much of how to use it in this case. The only examples I could find were for string operations. Can someone please help me out? 回答1: The totally simple one liner: NSSet *uniqueStates = [NSSet setWithArray:[myArrayOfCustomObjects valueForKey:@"state"]]; The trick is

NSArray creation with variable argument lists

ぐ巨炮叔叔 提交于 2019-12-17 16:53:00
问题 This array convenience method takes a comma-separated list of objects ending with nil . myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil]; What is the purpose of the nil ? 回答1: Null terminated variable argument lists , or va_list s, keep walking the list of arguments until they encounter a placeholder or sentinel, which is nil . Since the method has no way of knowing how many arguments you are passing, it needs the sentinel ( nil ) to tell where the list ends. 回答2: To mark the

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

谁都会走 提交于 2019-12-17 15:42:13
问题 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... 回答1: It is actually not

removing duplicates from array in objective c

我是研究僧i 提交于 2019-12-17 11:44:12
问题 I have an array with custom objects. Each array item has a field named "name". Now I want to remove duplicate entries based on this name value. How should I go about achieving this. Thanks in advance. 回答1: You might have to actually write this filtering method yourself: @interface NSArray (CustomFiltering) @end @implementation NSArray (CustomFiltering) - (NSArray *) filterObjectsByKey:(NSString *) key { NSMutableSet *tempValues = [[NSMutableSet alloc] init]; NSMutableArray *ret =

How to sort NSArray of objects based on one attribute

人盡茶涼 提交于 2019-12-17 10:56:31
问题 I am trying to sort an array of managed objects alphabetically. The attribue that they need to be sorted by is the name of the object (NSString) with is one of the managed attributes. Currently I am putting all of the names in an array of strings and then using sortedNameArray = [sortedNameArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; and then enumerating them back into an array with the objects. This falls apart when two names are the same so I really need to

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

☆樱花仙子☆ 提交于 2019-12-17 09:59:42
问题 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. 回答1: 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

Plist Array to NSDictionary

坚强是说给别人听的谎言 提交于 2019-12-17 09:47:21
问题 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<

What's the best way to put a c-struct in an NSArray?

两盒软妹~` 提交于 2019-12-17 07:02:11
问题 What's the usual way to store c-structures in an NSArray ? Advantages, disadvantages, memory handling? Notably, what's the difference between valueWithBytes and valueWithPointer -- raised by justin and catfish below. Here's a link to Apple's discussion of valueWithBytes:objCType: for future readers... For some lateral thinking and looking more at performance, Evgen has raised the issue of using STL::vector in C++ . (That raises an interesting issue: is there a fast c library, not unlike STL:

Convert an iOS objective c object to a JSON string

倾然丶 夕夏残阳落幕 提交于 2019-12-17 06:39:19
问题 I have an objective C class like, @interface message : NSObject { NSString *from; NSString *date; NSString *msg; } I have an NSMutableArray of instances of this message class. I want serialize all the instances in the NSMutableArray into a JSON file, using the new JSONSerialization APIs in iOS 5 SDK. How can I do this ? Is creating a NSDictionary of each key, by iterating through each instance of the elements in the NSArray ? Can someone help with code of how to solve this ? I am not able to

Which has faster performance indexesOfObjectsPassingTest or filteredArrayUsingPredicate?

≯℡__Kan透↙ 提交于 2019-12-17 05:45:09
问题 When needing to filter an NSArray to get a subset of the items in the array returned, which method is quicker more frequently and in edge cases? 回答1: The following tests (compiled in Release mode, executed on a Mac Pro) indicate that filteredArrayUsingPredicate is slower than indexesOfObjectsPassingTest if you use a "textual" predicate, but faster if you use block-based predicate. The fasted method in my test was a simple (fast-enumeration) loop that adds all matching objects to a mutable