nsarray

Searching NSArray of NSDictionary objects

走远了吗. 提交于 2019-11-26 16:35:18
问题 I've contacts array where each contact is a dictionary. Each contact has a key "contact_type" which is an NSNumber. Contact type basically represents whether its a Facebook, LinkedIn or Mail contact etc. I've a search array which holds only NSNumber of contact_type's. What I want is to make a temporary array of contacts which I want to search using my search array. I am facing trouble using NSPredicate to create searched contacts array. Can some one guide me on how to do it? 回答1: NSArray

Objective-C中NSArray类的解读

旧城冷巷雨未停 提交于 2019-11-26 16:01:07
Objective-C中NSArray类的解读 NSArray数组类是Objective-C语言中常用的也是重要的一个类,除了开发中常用到的一些基础功能,NSArray及其相关类中还封装了许多更加强大的功能。有机会总结了一下,与需要的朋友们分享。 NSArray中属性与方法: //获取数组中元素个数 @property (readonly) NSUInteger count; //通过下标获数组中的元素 - (ObjectType)objectAtIndex:(NSUInteger)index; //初始化方法 - (instancetype)init; //通过C语言风格的数组创建NSArray对象 需要注意,C数组中需要为Objective对象,cnt参数为C数组的长度 //如果cnt的值小于C数组的长度,则会对C数据进行截取赋值,如果大于则程序会崩溃 - (instancetype)initWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt; //数组的归档方法 - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; //像数组中追加一个元素 这个方法会返回一个新的数组 - (NSArray<ObjectType> *

Convert NSArray to NSDictionary

自古美人都是妖i 提交于 2019-11-26 15:59:40
问题 How can I convert an NSArray to an NSDictionary , using an int field of the array's objects as key for the NSDictionary ? 回答1: - (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array { id objectInstance; NSUInteger indexKey = 0U; NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; for (objectInstance in array) [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]]; return (NSDictionary *)[mutableDictionary autorelease

What is __NSArrayI and __NSArrayM? How to convert to NSArray?

主宰稳场 提交于 2019-11-26 15:52:59
问题 What is __NSArrayI and __NSArrayM? __NSArrayI(or M) cause "unrecognized selector" error. How to convert to NSArray? I did test to parse json, twitter api. http://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=twitterapi ==> works fine. parsed object is NSCFDictionary class. (This dictionary contains __NSArrayM class) http://api.twitter.com/1/statuses/user_timeline.json?&screen_name=twitterapi ==> error. parsed object is __NSArrayM class. 回答1: __NSArrayI is a code-word for an

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

淺唱寂寞╮ 提交于 2019-11-26 14:39:42
问题 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

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

泪湿孤枕 提交于 2019-11-26 14:38:12
问题 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; 回答1: if ([[NSSet setWithArray:array1] isEqualToSet:[NSSet setWithArray:array2]]) { // the objects are the same } 回答2: You can use NSCountedSet for that purpose: - (BOOL)isSameValues:(NSArray*)array1 and:(NSArray*)array2

Finding smallest and biggest value in NSArray of NSNumbers

若如初见. 提交于 2019-11-26 14:17:32
What's an effective and great way to compare all the values of NSArray that contains NSNumbers from floats to find the biggest one and the smallest one? Any ideas how to do this nice and quick in Objective-C? If execution speed (not programming speed ) is important, then an explicit loop is the fastest. I made the following tests with an array of 1000000 random numbers: Version 1: sort the array: NSArray *sorted1 = [numbers sortedArrayUsingSelector:@selector(compare:)]; // 1.585 seconds Version 2: Key-value coding, using "doubleValue": NSNumber *max=[numbers valueForKeyPath:@"@max.doubleValue"

Best way to sort an NSArray of NSDictionary objects?

点点圈 提交于 2019-11-26 14:16:19
I'm struggling with trying to sort an array of dictionaries. My dictionaries have a couple of values of interest, price, popularity etc. Any suggestions? Warrior Use NSSortDescriptor like this.. NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES]; stories = [stories sortedArrayUsingDescriptors:@[descriptor]]; recent = [stories copy]; stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the @"interest" with the key value on which you have to sort. All the best superarts.org [array

Using NSRegularExpression to extract URLs on the iPhone

和自甴很熟 提交于 2019-11-26 12:45:37
问题 I\'m using the following code on my iPhone app, taken from here to extract all URLs from striped .html code. I\'m only being able to extract the first URL, but I need an array containing all URLs. My NSArray isn\'t returning NSStrings for each URL, but the objects descriptions only. How do I make my arrayOfAllMatches return all URLs, as NSStrings? -(NSArray *)stripOutHttp:(NSString *)httpLine { // Setup an NSError object to catch any failures NSError *error = NULL; // create the

What is an easy way to break an NSArray with 4000+ objects in it into multiple arrays with 30 objects each?

◇◆丶佛笑我妖孽 提交于 2019-11-26 12:44:17
问题 What is an easy way to break an NSArray with 4000 objects in it into multiple arrays with 30 objects each? So right now I have an NSArray *stuff where [stuff count] = 4133. I want to make a new array that holds arrays of 30 objects. What is a good way to loop through, breaking *stuff into new 30-object arrays, and placing them inside of a larger array? Obviously the last array won\'t have 30 in it (it will have the remainder) but I need to handle that correctly. Make sense? Let me know if