nsarray

How to turn an NSArray of strings into an array of unique strings, in the same order?

旧城冷巷雨未停 提交于 2019-11-29 21:26:17
If you have an NSArray of strings { @"ONE", @"ONE", @"ONE", "TWO", @"THREE", @"THREE" } How would I turn that into { @"ONE", @"TWO", @"THREE" } ..where the array follows the same order as the original. I think that you can turn an array into an NSSet to get unique items, but if you turn it back into an array you are not guaranteed to get the same order.. My initial thought was that you could do: NSArray * a = [NSArray arrayWithObjects:@"ONE", @"ONE", @"ONE", @"TWO", @"THREE", @"THREE", nil]; NSLog(@"%@", [a valueForKeyPath:@"@distinctUnionOfObjects.self"]); But that does not maintain order.

How can I check if an object in an NSArray is NSNull?

戏子无情 提交于 2019-11-29 21:12:43
I am getting an array with null value. Please check the structure of my array below: ( "< null>" ) When I'm trying to access index 0 its crashing because of -[NSNull isEqualToString:]: unrecognized selector sent to instance 0x389cea70 Currently its crashing because of that array with a crash log: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x389cea70' *** First throw call stack: (0x2d9fdf53 0x3820a6af 0x2da018e7 0x2da001d3 0x2d94f598 0x1dee57 0x1dfd31 0x302f598d 0x301a03e3 0x3052aeed

Get all keys of an NSDictionary as an NSArray

做~自己de王妃 提交于 2019-11-29 21:10:13
Is it possible to get all the keys from a specific NSDictionary as a seperate NSArray ? Just use NSArray*keys=[dict allKeys]; In general, if you wonder if a specific class has a specific method, look up Apple's own documentation . In this case, see NSDictionary class reference. Go through all the methods. You'll discover many useful methods that way. Yes it's possible. Use allKeys method: NSDictionary *yourDictionary; NSArray * yourKeys yourKeys = [yourDictionary allKeys]; And if you want to get all keys and values , here's what you do: for (NSString *key in dictionary) { id value = dictionary

Making an array of Objects in Objective-C.

核能气质少年 提交于 2019-11-29 20:29:27
Been playing around with XCode for about 2 weeks now, and reading about MVC a bit. I have a problem trying to connect the Model to the Controller because I find it hard to get my head around arrays. I can handle simple arrays when i programmed some in Java but I'm quiet intimidated by the Obj-C NSArrays I see. If somebody would be so kind to show me some simple calls on an array of objects i would be eternally grateful. My model: Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { NSString *name; NSNumber *age; } @property(nonatomic, retain) NSString *name; @property

Check if NSString instance is contained in an NSArray

对着背影说爱祢 提交于 2019-11-29 20:06:43
I have an array with a bunch of strings and I want to check if a certain string is contained in the array. If I use the containsObject : message on the array, I'm getting correct results. Do all NSString objects with the same string point to the same object? Or why is the containsObject : working? NSArray *stringArray = [NSArray arrayWithObjects:@"1",@"2",@"3",anotherStringValue, nil]; if([stringArray containsObject:@"2"]){ //DO SOMETHING } Yes, hard-coded NSStrings (string literals) (that is any @"..." in your source code) are turned into strings that exist indefinitely while your process is

How to get indexPath.row of tableView which is currently being displayed?

雨燕双飞 提交于 2019-11-29 18:37:30
问题 I have a tableView with many values. I want to get the first indexPath.row value of the table which is being displayed currently. How can I do that? I get Following Error while implementing krishnabhadra's answer: Line at which the error comes is: [self.table scrollToRowAtIndexPath:indexVis atScrollPosition:UITableViewScrollPositionTop animated:YES]; ERROR: Assertion failure in -[NSIndexPath row], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableViewSupport.m:2018 2011-04-01 11:57:25.458

Retrieve NSDictionary from NSArray where dictionary key has value X

谁都会走 提交于 2019-11-29 18:12:46
问题 I have an NSArray with NSDictionaries . One of the dictionaries keys in one of the arrays contains a value. I want to retrieve the NSDictionary with that value. My array: Array: ( { DisplayName = "level"; InternalName = "Number 2"; NumberValue = 1; }, { DisplayName = "PurchaseAmount"; InternalName = "Number 1"; NumberValue = 3500; } ) So, I would like to get the dictionary which contains DisplayName set to PurchaseAmount (case insensitive). How can I accomplish that? 回答1: LIKE[cd] will also

How retrieve an index of an NSArray using a NSPredicate?

浪尽此生 提交于 2019-11-29 17:37:19
问题 I would know how retrieve an index of an NSArray using a NSPredicate ? NSArray *array = [NSArray arrayWithObjects: @"New-York City", @"Washington DC", @"Los Angeles", @"Detroit", nil]; Wich kind of method should I use in order to get the index of "Los Angles" by giving only a NSString NB: @"Los An" or @"geles" should return the same index.. 回答1: Using NSPredicate you can get array of strings that contain your search string (it seems there's no built-in method to get just element indexes):

Finding a particular instance in an array by a property

梦想与她 提交于 2019-11-29 17:22:56
I have an array containing various instances of MyClass , which has four properties: int id , int valueA , int valueB , and int valueC . On occasion, I need to modify a property for a specific instance (for this example let's say it is the one with id equal to 5 ). Currently I do it like this: MyClass *myClass = [[MyClass alloc] init]; for (int i = 0; i < [myMutableArray count]; i++) { myClass = [myMutableArray objectAtIndex:i]; if(myClass.id == 5) { myClass.valueA = 100; myClass.valueB = 200; myClass.valueC = 300; [myMutableArray replaceObjectAtIndex:i withObject: myClass]; } } Is there a

Save NSArray of Class to cacheDirectory

一世执手 提交于 2019-11-29 16:53:54
I'd like to NSArray of Class to the cacheDirectory. I've wrote as following, however it returns false. Could you tell me how to solve this problem? Thank you for your kindness. let paths2 = NSSearchPathForDirectoriesInDomains( .CachesDirectory, .UserDomainMask, true) let cachesPath: AnyObject = paths2[0] var cachedQuestions:NSArray = questions as NSArray let filePath = cachesPath.stringByAppendingPathComponent("CachedQuestions") class Dog { var id:Int? var name:String? init(id:Int, name:String) { self.id = id self.name = name } } var dogs = [Dog]() dogs.append(Dog(id:1, name:"taro")) dogs