nsarray

NSArray: Remove objects with duplicate properties

瘦欲@ 提交于 2019-11-28 23:40:52
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 = @"Steve Johnson"; [items addObject:asset]; [asset release]; // Third asset = [[Asset alloc] init]; asset

NSArray + remove item from array

筅森魡賤 提交于 2019-11-28 22:22:24
How to remove an item from NSArray. luvieere NSArray is not mutable, that is, you cannot modify it. You should take a look at NSMutableArray . Check out the "Removing Objects" section, you'll find there many functions that allow you to remove items: [anArray removeObjectAtIndex: index]; [anArray removeObject: item]; [anArray removeLastObject]; NSMutableArray *arrayThatYouCanRemoveObjects = [NSMutableArray arrayWithArray:your_array]; [arrayThatYouCanRemoveObjects removeObjectAtIndex:your_object_index]; [your_array release]; your_array = [[NSArray arrayWithArray: arrayThatYouCanRemoveObjects]

[__NSArrayM insertObject:atIndex:]: object cannot be nil - how determine where is the error?

梦想的初衷 提交于 2019-11-28 21:42:03
问题 I have big project with async event, and sometimes i have error [__NSArrayM insertObject:atIndex:]: object cannot be nil , but I havent idea where is throwing this error.. How can I catch this error? I make assert in everywhere where I make operation insertobject:atIndex and nothing.. This is stack: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: (0x34c352a3

What's the difference between a dictionary and an array?

假装没事ソ 提交于 2019-11-28 21:26:30
What is the difference between a dictionary and an array, especially when working with PLIST files? What are the advantages of using one over the other? Thanks! Both NSDictionary and NSArray are collection classes, i.e. the group together other objects. An NSArray is an 'ordered collection' - every item in the collection has an integer index, so there is an explicit order to the items. If you swap the order of items in the collection then the collection is no longer the 'same' as the order is different. An object may appear more than once in the collection. An NSSet is an 'unordered collection

How to stop enumerateObjectsUsingBlock Swift

扶醉桌前 提交于 2019-11-28 21:09:42
How do I stop a block enumeration? myArray.enumerateObjectsUsingBlock( { object, index, stop in //how do I stop the enumeration in here?? }) I know in obj-c you do this: [myArray enumerateObjectsUsingBlock:^(id *myObject, NSUInteger idx, BOOL *stop) { *stop = YES; }]; Christian Dietrich In Swift 1: stop.withUnsafePointer { p in p.memory = true } In Swift 2: stop.memory = true In Swift 3 - 4: stop.pointee = true This has unfortunately changed every major version of Swift. Here's a breakdown: Swift 1 stop.withUnsafePointer { p in p.memory = true } Swift 2 stop.memory = true Swift 3 stop.pointee

Sorting NSArray which many contain number

对着背影说爱祢 提交于 2019-11-28 20:43:10
I have an NSArray which is populated with objects from an NSMutableArray. Most of these object have integer values like "1", "2", "3", "4", "5", sometimes there is a name like "home", "far left", or "far right". I am trying to sort this array in Objective C. using sortedArrayUsingSelector:@selector(compare:) works fine when I have less then 10 items in the array. but when it there are more I start getting "1", "10", "11", "12", "2", "3" type of stuff. Any help would be most appreciated. The code should not return anything. It just needs to sort and move on. Original Code: presetNamesSort = [[

Sort NSArray with sortedArrayUsingComparator

≡放荡痞女 提交于 2019-11-28 19:15:56
In Objective-C I can sort an NSArray using this statement: NSArray *sortedArray = [persons sortedArrayUsingComparator:^NSComparisonResult(Person *p1, Person *p2) { return [p1.name compare:p2.name]; }]; I'm unable to reproduce the same statement with Swift . All I found was using Array . Mike S You can either use Swift's built in sort functions or, since a Swift array is bridged to NSArray you can call sortedArrayUsingComparator from swift directly. Using Swift's sorted function: var sortedArray = sorted(persons) { (obj1, obj2) in // The downcast to Person is only needed if persons is an

Converting an NSArray of Dictionaries to JSON array in iOS

空扰寡人 提交于 2019-11-28 17:37:40
I need to send an NSArray to the server in the JSON array format. How can I convert it to JSON . This is a sample of my NSArray that I have to pass. array([0] => array('latitude'=>'10.010490', 'longitude'=>'76.360779', 'altitude'=>'30.833334', 'timestamp'=>'11:17:23', 'speed'=>'0.00', 'distance'=>'0.00'); [1] => array('latitude'=>'10.010688', 'longitude'=>'76.361378', 'altitude'=>'28.546305', 'timestamp'=>'11:19:26', 'speed'=>'1.614', 'distance'=>'198.525711') )` and the required format is like this [ { "latitude":"10.010490", "longitude":"76.360779", "altitude":"30.833334", "timestamp":"11:17

Search through NSArray for string

一世执手 提交于 2019-11-28 17:05:38
I would like to search through my NSArray for a certain string. Example: NSArray has the objects: "dog", "cat", "fat dog", "thing", "another thing", "heck here's another thing" I want to search for the word "another" and put the results into one array, and have the other, non results, into another array that can be filtered further. Ken Aspeslagh Not tested so might have a syntax error, but you'll get the idea. NSArray* inputArray = [NSArray arrayWithObjects:@"dog", @"cat", @"fat dog", @"thing", @"another thing", @"heck here's another thing", nil]; NSMutableArray* containsAnother =

Getting Index of an Object from NSArray?

血红的双手。 提交于 2019-11-28 16:56:45
i am trying to get index of an array through indexOfObject method as follows but when i try to log the value to test the index i get a garbage value.. for testing purposes i am having an array with values {57,56,58..} to get an index of lets say 56 , NSNumber *num = [NSNumber numberWithInteger:56]; NSInteger Aindex = [myArray indexOfObject:num]; NSLog(@" %d",Aindex); the value i get is something like 2323421 . what am i possibly doing wrong?? The index returned by indexOfObject will be the first index for an occurence of your object. Equality is tested using isEqual method. The garbage value