nsarray

Converting NSArray to NSMutableArray [closed]

我们两清 提交于 2019-12-02 17:07:48
As above. Would be helpful to know. Thanks! Here are two options: - (NSMutableArray *)createMutableArray1:(NSArray *)array { return [NSMutableArray arrayWithArray:array]; } - (NSMutableArray *)createMutableArray2:(NSArray *)array { return [[array mutableCopy] autorelease]; } Use -mutableCopy and make sure to balance the retain count. NSMutableArray *mutableArray = [[immutableArray mutableCopy] autorelease]; dhaya Try this one NSMutableArray *array = [[NSMutableArray alloc]initWithArray:your_array_name]; 来源: https://stackoverflow.com/questions/3222122/converting-nsarray-to-nsmutablearray

Parsing Json to get all the contents in one NSArray

不羁岁月 提交于 2019-12-02 16:56:31
问题 That's the content... [ { "id": "", "title": "", "website": "", "categories": [ { "id": "", "label": "" } ], "updated": }, { "id": "", "title": "", "website": "", "categories": [ { "id": "", "label": "" } ], "updated": } ] How can I insert every feed source in one array? NSDictionary *results = [string JSONValue]; NSArray *subs = [results valueForKey:@"KEY"]; Which key I must insert? THanks 回答1: as I can see your structure, you will get out of this JSON-String NSArray: [ NSDictionary: {

Why does a (copy, nonatomic) NSMutableArray property create NSArrays?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 16:43:37
I made a mistake while creating a TableView class , and accidentally kept my @property as copy when I defined it: @property (copy, nonatomic) NSMutableArray *words; I initialised the array "correctly": (note this is the third attempt, so please ignore the fact that I'm not using mutableCopy and other better ways of doing this) NSArray *fixedWords = @[@"Eeny", @"Meeny", @"Miny", @"Moe", @"Catch", @"A", @"Tiger", @"By", @"His", @"Toe"]; NSMutableArray *mutWords = [[NSMutableArray alloc] initWithArray:fixedWords]; self.words = mutWords; However when I later came to reorder the array, it crashed

Beautify NSLog of NSArray & NSDictionary

…衆ロ難τιáo~ 提交于 2019-12-02 16:35:06
I'm dealing with deeply nested NSArray's and NSDictionary's and it's very time consuming to say the least. [data objectatindex:0] valueForKey:@"blah"] etc etc Does anyone know of a nice iOS category to recursively log the structure, highlight the type and show the values? Might be asking a bit much but you never know :) Maybe like this? for (id key in dictionary) { NSLog(@"key: %@, value: %@ \n", key, [dictionary objectForKey:key]); } but i can't think of any nice way of getting the output beautiful except copy & paste it into a jsonFormatter (for example) EDIT : @Andrey Starodubtsev has the

How to Covert struct with an Array of string to NSData and vice versa Swift

不羁的心 提交于 2019-12-02 16:29:59
问题 I have this struct: struct MessageRandomWords { let message = MessageType.kMessageTypeRandomWords let randomWords : Array<Array<String>> } I'm trying to convert this struct to NSDate by doing this: var message = MessageRandomWords(randomWords: self.words) let data = NSData(bytes: &message, length: sizeof(MessageRandomWords)) But when i'm trying to convert this back to the original struct: var messageRandomWords : MessageRandomWords? data.getBytes(&messageRandomWords, length: sizeof

Creating an array from properties of objects in another array

只愿长相守 提交于 2019-12-02 16:09:18
Is there any convenient way to take an array/set of objects and create a new array/set containing some property of each item in the first array? For example, an array contains Car objects. I need an array of licensePlates, where each car has an NSObject car.licensePlate. Currently I just iterate through the first array adding objects to my mutable results array, but was wondering if there is an instantiation method that exists for this (checked the docs for NSArray). This will return an array containing the value of licensePlate from each item in the myCars array: NSArray *licensePlates =

How to return an NSMutableArray from an NSSet

北慕城南 提交于 2019-12-02 16:02:22
I'm able to put the contents of an NSSet into an NSMutableArray like this: NSMutableArray *array = [set allObjects]; The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray . How should this be fixed? Since -allObjects returns an array, you can create a mutable version with: NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]]; Or, alternatively, if you want to handle the object ownership: NSMutableArray *array = [[set allObjects] mutableCopy]; I resolved crashing by using NSMutableArray's method 'addObjectsFromArray' to assign all

Objective-C, sorting muti-dimensional arrays

感情迁移 提交于 2019-12-02 13:57:23
问题 I've been having some trouble with sorting multi-dimensional arrays in Objective-C. I basically have an array of which every element is an array of the form: (NSString, NSDate, NSString, NSString) such that my top level array has the form: ( (NSString, NSDate, NSString, NSString), (NSString, NSDate, NSString, NSString), (NSString, NSDate, NSString, NSString), (NSString, NSDate, NSString, NSString), ... ) I would like to be able to sort the elements of the top level array based on any of of

Fetch last five values from NSMutableArray

房东的猫 提交于 2019-12-02 13:21:13
I have a NSMutableArray whose count is 10 and I want to extract the last 5 values and store them in another array. How can I do this? I think that - (NSArray *) subarrayWithRange:(NSRange)range ( doc ) might help you. NSRange theRange; theRange.location = [wholeArray count] - 5; theRange.length = 5; NSArray *result = [wholeArray subarrayWithRange:theRange]; EDIT : Be careful to check that your array has at least five elements, else, subarrayWithRange will throw an exception. Use following way : NSMutableArray *first = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@

What does this Objective-C dictionary code do?

穿精又带淫゛_ 提交于 2019-12-02 12:27:09
.h @property (nonatomic, strong) NSMutableDictionary * products; //not synthesized in .m .m - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { NSLog(@"Loaded list of products..."); _productsRequest = nil; NSArray * skProducts = response.products; for (SKProduct * skProduct in skProducts) { IAPProduct * product = _products[skProduct.productIdentifier]; product.skProduct = skProduct; product.availableForPurchase = YES; } for (NSString * invalidProductIdentifier in response.invalidProductIdentifiers) { IAPProduct * product = _products