nsarray

What is the NSObject isEqual: and hash default function?

佐手、 提交于 2019-11-27 17:25:37
问题 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

Using NSPredicate to determine if a string equals another string

岁酱吖の 提交于 2019-11-27 17:07:22
问题 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

How do I convert NSMutableArray to NSArray?

[亡魂溺海] 提交于 2019-11-27 16:36:09
How do I convert NSMutableArray to NSArray in objective-c ? NSArray *array = [mutableArray copy]; Copy makes immutable copies. This is quite useful because Apple can make various optimizations. For example sending copy to a immutable array only retains the object and returns self . If you don't use garbage collection or ARC remember that -copy retains the object. hallski An NSMutableArray is a subclass of NSArray so you won't always need to convert but if you want to make sure that the array can't be modified you can create a NSArray either of these ways depending on whether you want it

what does -arrayWithArray actually DO?

丶灬走出姿态 提交于 2019-11-27 16:36:01
问题 I want to see EXACTLY how it creates an array. How can I view the .m files that show how it's done? 回答1: As @Ken mentioned, you can't see the source (although you can disassemble the method via gdb). The method itself creates an immutable (can't be changed), autoreleased copy of the given array. The following are identical in behavior: // Both resulting arrays are immutable and won't be retained NSArray* immutableArray = [[[NSArray alloc] initWithArray:mutableArray] autorelease]; NSArray*

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

让人想犯罪 __ 提交于 2019-11-27 16:26:44
问题 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? 回答1: Yes, use the NSArray -valueForKey: method. NSArray *extracted = [sourceArray valueForKey:@"a key"]; 回答2: Yes, just use Key-Value Coding to ask for the values of the key: NSArray* names = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @"Joe",@"firstname", @"Bloggs",@"surname",

Getting an array of a property from an object array

匆匆过客 提交于 2019-11-27 16:20:23
I need to extract an array of a single property from a custom object array. eg. @interface MyClass : NSObject { int sampleNumber; NSString *sampleName; } I have an array of MyClass instances called myArray . I want to then get an array of the sampleName strings. Is there a way to do it without stepping through the whole array like this: NSMutableArray *stringArray; for (MyClass *thisInstance in myArray) { [stringArray addObject:thisInstance.sampleName]; } I attempted to search for a similar question in Objective-C but found it only in the PHP and LINQ sections. Use Key-Value Coding : NSArray

Using @[array, of, items] vs [NSArray arrayWithObjects:]

你离开我真会死。 提交于 2019-11-27 15:32:00
Is there a difference between NSArray *myArray = @[objectOne, objectTwo, objectThree]; and NSArray *myArray = [NSArray arrayWithObjects:objectOne, objectTwo, objectThree, nil]; Is one preferred over the other? They are almost identical, but not completely. The Clang documentation on Objective-C Literals states: Array literal expressions expand to calls to +[NSArray arrayWithObjects:count:] , which validates that all objects are non-nil. The variadic form, +[NSArray arrayWithObjects:] uses nil as an argument list terminator, which can lead to malformed array objects. So NSArray *myArray = @

How to convert NSArray into NSString

Deadly 提交于 2019-11-27 15:13:13
问题 How to convert NSArray into NSString in objective-c? 回答1: 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: @","] 回答2: 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. 回答3: This is how I

Generating permutations of NSArray elements

三世轮回 提交于 2019-11-27 15:07:32
问题 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? 回答1: 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

NSArray: Remove objects with duplicate properties

你说的曾经没有我的故事 提交于 2019-11-27 14:56:11
问题 I have an NSMutableArray that contains a few custom objects. Two of the objects have the same properties such as title and author. I want to remove the duplicate object and leave the other. Asset *asset; NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease]; // First asset = [[Asset alloc] init]; asset.title = @"Developer"; asset.author = @"John Smith"; [items addObject:asset]; [asset release]; // Second asset = [[Asset alloc] init]; asset.title = @"Writer"; asset.author = @