nsarray

Best way to save to nsuserdefaults for custom class?

有些话、适合烂在心里 提交于 2019-11-28 05:59:33
If I have a custom class Person which has three variables (which are propertized and synthesized): NSString* theName; float* theHeight; int theAge; Person instances are stored in an NSArray 'Group'. There is only one Group. What is the best way of storing and loading the Group in NSUserDefaults? (bearing in mind that float and int are not legal for NSUserDefaults) @Brad Smith's answer is not complete, and even is incorrect in some sense. We have to use NSKeyedArchiver and NSKeyedUnarchiver as follows: Make your class (for example in this case Person ) to conform to protocol NSCoding and

Check if array contains part of a string in Swift?

◇◆丶佛笑我妖孽 提交于 2019-11-28 05:59:10
I have an array containing a number of strings. I have used contains() (see below) to check if a certain string exists in the array however I would like to check if part of a string is in the array? itemsArray = ["Google, Goodbye, Go, Hello"] searchToSearch = "go" if contains(itemsArray, stringToSearch) { NSLog("Term Exists") } else { NSLog("Can't find term") } The above code simply checks if a value is present within the array in its entirety however I would like to find "Google, Google and Go" Try like this. let itemsArray = ["Google", "Goodbye", "Go", "Hello"] let searchToSearch = "go" let

Disadvantage of using NSMutableArray vs NSArray?

五迷三道 提交于 2019-11-28 05:46:10
I'm using an array to store cached objects loaded from a database in my iPhone app, and was wondering: are there any significant disadvantages to using NSMutableArray that I should know of? edit: I know that NSMutableArray can be modified, but I'm looking for specific reasons (performance, etc..) why one would use NSArray instead. I assume there would be a performance difference, but I have no idea whether it's significant or not. If you're loading objects from a database and you know exactly how many objects you have, you would likely get the best performance from NSMutableArray s

Using NSPredicate to filter based on multiple keys (NOT values for key)

邮差的信 提交于 2019-11-28 05:33:50
I have the following NSArray containing NSDictionary(s): NSArray *data = [[NSArray alloc] initWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"bill", [NSNumber numberWithInt:2], @"joe", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:3], @"bill", [NSNumber numberWithInt:4], @"joe", [NSNumber numberWithInt:5], @"jenny", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:6], @"joe", [NSNumber numberWithInt:1], @"jenny", nil], nil]; I am wanting to create a filtered NSArray that only contains objects where the

Comma-separated string to NSArray in Objective-C

寵の児 提交于 2019-11-28 05:15:13
So I have no experience with arrays... But I need to use one to populate a UIPickerView. I am obtaining a list of objects via HTTP (NSURLConnection). This works fine. Currently, the response is stored in a NSString as a comma-separated list. I need to convert it to an array. I think this is the type of array I need: NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil]; Maybe I'm overcomplicating things... I'm really not sure. There is already an array for the PickerView, and I have it setup so to add an item to the PickerView array I use this code: [pickerArray addObject:@

Loading images for animation in UIImageView - iOS

China☆狼群 提交于 2019-11-28 04:59:25
问题 I have around 300 images to be loaded for animation the images are named loading001.png, loading002.png, loading003.png, loading004.png………loading300.png I am doing it in the following way. .h file #import <UIKit/UIKit.h> @interface CenterViewController : UIViewController { UIImageView *imgView; } @end .m file @implementation CenterViewController - (void)viewDidLoad { [super viewDidLoad]; imgView = [[UIImageView alloc] init]; imgView.animationImages = [[NSArray alloc] initWithObjects: [UIImage

Deep Copy and Shallow Copy

 ̄綄美尐妖づ 提交于 2019-11-28 04:35:13
I have read the answer for difference between deep copy and shallow copy from the post, What is the difference between a deep copy and a shallow copy? . Now I got some doubt that when we made a shallow copy by newArray = [NSmutableArray arrayWithArray:oldArray]; the new array will point to oldArray. (As from the figure). Now what happen when I remove object from newArray? As from figure, it should remove same element from oldArray too !!! It seems like newArray = oldArray is a shallow copy and newArray = [NSmutableArray arrayWithArray:oldArray]; is deep copy. Is it right? yuji newArary =

NSArray with NSPredicate using NOT IN

心已入冬 提交于 2019-11-28 04:26:48
I have an NSArray that I want to filter out certain objects using an NSPredicate, I was hoping I could use NOT IN since I saw that I can easily do an IN. So I have my array: self.categoriesList Then I get the values I want to remove: NSArray *parentIDs = [self.cateoriesList valueForKeyPath:@"@distinctUnionOfObjects.ParentCategoryID"]; This gives me a list of ParentCategoryID's for categories I DO NOT want to display, so I figure I can use an NSPredicate to remove them: self.cateoriesList = [self.cateoriesList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"CategoryID NOT IN %@"

arrayWithContentsOfFile can't read plist file

喜欢而已 提交于 2019-11-28 04:04:51
问题 i have a plist with an array that contain 2 strings see image : http://imageshack.us/photo/my-images/263/myplist.png/ and in my viewDidLoad i add this : NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; NSArray *array = [NSArray arrayWithContentsOfFile:file]; NSLog(@"array = %@", array); But i got always null why? Thanks for help. UPDATE : the solution is that you need to manually edit the plist file (Xcode4 use Dictionnary by default) <?xml version="1.0"

iPhone - getting unique values from NSArray object

久未见 提交于 2019-11-28 02:49:06
I have an NSArray formed with objects of a custom class. The class has 3 (city, state, zip) string properties. I would like to get all unique state values from the array . I did read through the NSPredicate class but couldn't make much of how to use it in this case. The only examples I could find were for string operations. Can someone please help me out? The totally simple one liner: NSSet *uniqueStates = [NSSet setWithArray:[myArrayOfCustomObjects valueForKey:@"state"]]; The trick is the valueForKey: method of NSArray . That will iterate through your array ( myArrayOfCustomObjects ), call