nsarray

NSPredicate subquery syntax

浪子不回头ぞ 提交于 2019-11-29 03:55:21
问题 I have sort of an unfriendly array of dictionaries that in turn have arrays of data and I am trying to filter the outer array based on any of the inner array passing a predicate. I can't seem to create an NSPredicate to make this work. I started off with: NSPredicate *lookupPredicate = [NSPredicate predicateWithFormat: @"row_values.property_id == %@ AND row_values.property_value == %@", @"47cc67093475061e01000540", @"Male"]; [dataRows filterUsingPredicate:lookupPredicate]; This returns no

Get NSIndexSet from NSArray

雨燕双飞 提交于 2019-11-29 03:25:26
问题 NSArray has useful methods to find objects for specified indexes // To find objects by indexes - (id)objectAtIndex:(NSUInteger)index - (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes // To find index by object - (NSUInteger)indexOfObject:(id)anObject However, I want to get NSIndexSet (multiple indexes) for given objects. Something like: - (NSIndexSet *)indexesOfObjects:(NSArray *)objects This method does not exist for NSArray . Am I missing something? Does someone know another standard

Sorting an NSArray of NSString

為{幸葍}努か 提交于 2019-11-29 03:03:15
Can someone please show me the code for sorting an NSMutableArray? I have the following NSMutableArray: NSMutableArray *arr = [[NSMutableArray alloc] init]; with elements such as "2", "4", "5", "1", "9", etc which are all NSString. I'd like to sort the list in descending order so that the largest valued integer is highest in the list (index 0). I tried the following: [arr sortUsingSelector:@selector(compare:)]; but it did not seem to sort my values properly. Can someone show me code for properly doing what I am trying to accomplish? Thanks! It's pretty simple to write your own comparison

What is the NSObject isEqual: and hash default function?

╄→гoц情女王★ 提交于 2019-11-29 02:54:50
I have a database model class that is a NSObject . I have a set of these objects in a NSMutableArray . I use indexOfObject: to find a match. Problem is the model object's memory address changes. So I am overriding the hash method to return the model's row ID. This however does not fix it. I also have to override the isEqual: method to compare the value of the hash method. What does the isEqual: method use to determine equality by default? I'm assuming it uses the memory address. After reading the isEqual: documentation I thought it used the value from the hash method. Obviously, that is not

Using NSPredicate to determine if a string equals another string

十年热恋 提交于 2019-11-29 02:50:23
I have an NSArray of CalEvents returned with the [CalCalendarStore eventPredicateWithStartDate] method. From the events returned, I am trying to keep only those in which the title of the event == @"on call" (case-insensitive). I am able to keep in the array those events whose title includes @"on call" with the following code (where 'events' is a 'NSArray' populated with CalEvents ): NSPredicate *onCallPredicate = [NSPredicate predicateWithFormat:@"(SELF.title CONTAINS[c] 'on call')"]; [events filteredArrayUsingPredicate:onCallPredicate]; I've tried using a predicate format string like: @"SELF

Fastest way to check if an array contains the same objects of another array

孤街浪徒 提交于 2019-11-29 01:55:52
问题 The goal is to compare two arrays as and check if they contain the same objects (as fast as possible - there are lots of objects in the arrays). The arrays cannot be checked with isEqual: as they are differently sorted. I already tried the solution posted here (https://stackoverflow.com/a/1138417 - see last code snippet of the post by Peter Hosey). But this doesn't work with differently sorted arrays. The code I'm using now is the following: + (BOOL)arraysContainSameObjects:(NSArray *)array1

From array of dictionaries, make array containing values of one key

杀马特。学长 韩版系。学妹 提交于 2019-11-29 01:49:12
I have an array of dictionaries. I would like to extract an array with all the elements of one particular key of the dictionaries in the original array. Can this be done without enumeration? Yes, use the NSArray -valueForKey: method. NSArray *extracted = [sourceArray valueForKey:@"a key"]; Yes, just use Key-Value Coding to ask for the values of the key: NSArray* names = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @"Joe",@"firstname", @"Bloggs",@"surname", nil], [NSDictionary dictionaryWithObjectsAndKeys: @"Simon",@"firstname", @"Templar",@"surname", nil],

UITableView Section Headers by month of dates in array

主宰稳场 提交于 2019-11-29 01:36:38
问题 I have an NSArray with something similar to: 6/1/13 | Data 6/2/13 | Data 7/1/13 | Data 9/1/13 | Data What I need to somehow get the months to create section headers - but only if they are in the array and then break the dates up into the appropriate sections. Looking like: (Section Header)June 2013 6/1/13 | Data 6/2/13 | Data (Section Header)July 2013 7/1/13 | Data (skips august as no dates from august are in array) (Section Header)September 2013 9/1/13 | Data I am attempting to implement: -

How to convert NSArray into NSString

天涯浪子 提交于 2019-11-29 00:09:58
How to convert NSArray into NSString in objective-c? Bearing in mind that you don't say what's in the NSArray, you can do this: NSArray *arr = [NSArray arrayWithObjects: @"foo", @"bar", @"baz"]; [arr componentsJoinedByString: @","] Aternatively to Frank's method, which works pretty well, you could also do NSString *myArrayString = [array description]; The default implementation of description on NSArray will print out the contents in a neatly formatted fashion. TheGreen This is how I have converted my NSArray to NSString NSError *error = nil; NSData *data = [NSJSONSerialization

Generating permutations of NSArray elements

杀马特。学长 韩版系。学妹 提交于 2019-11-28 23:55:53
Let's say I have an NSArray of NSNumbers like this: 1, 2, 3 Then the set of all possible permutations would look something like this: 1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1 What's a good way to do this in objective-c? I have used the code from Wevah's answer above and discovered some problems with it so here my changes to get it to work properly: NSArray+Permutation.h @interface NSArray(Permutation) - (NSArray *)allPermutations; @end NSArray+Permutation.m #import "NSArray+Permutation.h" #define MAX_PERMUTATION_COUNT 20000 NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger