nsarray

Convert NSString separated by comma to NSArray [duplicate]

烂漫一生 提交于 2019-11-27 02:50:28
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Convert NSArray to NSString in Objective-C I have an array of an NSArray myArray, here is the content: ( "<MPConcreteMediaItem: 0x1b7dd0> 8671085923283003246", "<MPConcreteMediaItem: 0x1b7e50> 16275751483823231324", "<MPConcreteMediaItem: 0x1b7e70> 4976771456698615367" ) I used the code below to assign to an NSString myString: NSString *myString = [myArray description]; And the out put is still as expected: ( "

What's the best way to put a c-struct in an NSArray?

故事扮演 提交于 2019-11-27 02:50:16
What's the usual way to store c-structures in an NSArray ? Advantages, disadvantages, memory handling? Notably, what's the difference between valueWithBytes and valueWithPointer -- raised by justin and catfish below. Here's a link to Apple's discussion of valueWithBytes:objCType: for future readers... For some lateral thinking and looking more at performance, Evgen has raised the issue of using STL::vector in C++ . (That raises an interesting issue: is there a fast c library, not unlike STL::vector but much much lighter, that allows for the minimal "tidy handling of arrays" ...?) So the

Filter Array with dictionaries using NSPredicate

╄→гoц情女王★ 提交于 2019-11-27 02:34:16
问题 There is an Array with each element being a NSDictionary . NSMutableArray *mutArr = [NSMutableArray array]; for (Person *person in persons) { NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:person.name, @"name", person.email, @"email", nil]; [mutArr addObject:dict]; } self.arr = [[NSArray alloc] initWithArray:mutArr]; How to filer the arr with name or email contains string @"filter string" by using filterArrayUsingPredicate: method. Thanks in advance! 回答1: Please see the

Pick random element of NSArray in Objective-C [duplicate]

流过昼夜 提交于 2019-11-27 02:27:28
问题 Possible Duplicate: Picking a Random Object in an NSArray I have have an array in Objective-C with strings: NSArray *tips; tips = [NSArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil]; I want a method that takes a random item from the array, and returns it. Is there a method, or how can I write one myself? Thanks. 回答1: Use this code: uint32_t rnd = arc4random_uniform([tips count]); NSString *randomObject = [tips objectAtIndex:rnd]; EDIT: While working on my project i decided to create a

how can I convert string to an array with separator?

末鹿安然 提交于 2019-11-27 02:01:29
问题 I have a string in the following format myString = "cat+dog+cow" I need to store each string separated by + in to a array. Eg: myArray[0] = cat myArray[1] = dog myArray[2] = cow Can anyone tell me the proper way to do this? 回答1: componentsSeparatedByString: splits the string and return the result in an array. NSArray *myArray = [myString componentsSeparatedByString:@"+"]; [myArray objectAtIndex:0];//cat [myArray objectAtIndex:1];//dog [myArray objectAtIndex:2];//cow 回答2: Try this.. NSArray

Convert an iOS objective c object to a JSON string

女生的网名这么多〃 提交于 2019-11-27 01:20:58
I have an objective C class like, @interface message : NSObject { NSString *from; NSString *date; NSString *msg; } I have an NSMutableArray of instances of this message class. I want serialize all the instances in the NSMutableArray into a JSON file, using the new JSONSerialization APIs in iOS 5 SDK. How can I do this ? Is creating a NSDictionary of each key, by iterating through each instance of the elements in the NSArray ? Can someone help with code of how to solve this ? I am not able to get good results in Google, as "JSON" skews the results to server-side calls and transfer of data

Best way to save to nsuserdefaults for custom class?

谁说我不能喝 提交于 2019-11-27 01:08:48
问题 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) 回答1: @Brad Smith's answer is not complete, and even is incorrect in some sense. We have to use NSKeyedArchiver and

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

浪尽此生 提交于 2019-11-27 00:58:14
问题 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], @

Comma-separated string to NSArray in Objective-C

南楼画角 提交于 2019-11-27 00:51:25
问题 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,

Filter array by first letter of string property

百般思念 提交于 2019-11-26 23:13:58
问题 I have an NSArray with objects that have a name property. I would like filter the array by name NSString *alphabet = [agencyIndex objectAtIndex:indexPath.section]; //---get all states beginning with the letter--- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; NSMutableArray *listSimpl = [[NSMutableArray alloc] init]; for (int i=0; i<[[Database sharedDatabase].agents count]; i++) { Town *_town = [[Database sharedDatabase].agents objectAtIndex:i];