nsarray

Who is responsible for releasing objects in an array when copying?

浪尽此生 提交于 2019-12-12 16:15:11
问题 In Objective-C, if array1 is copied onto array2 using mutableCopy, and suppose the code is done in main(), who is responsible for releasing the objects contained in the array? Is it main() or array2? 回答1: I think the previous answers have missed the point, or else the asker was pretty unclear. The actual question isn't talking about either array, but rather the array contents: who is responsible for releasing the objects contained in the array ? Is it main() or array2? Both array1 and array2

Returning an array without a Leak!

↘锁芯ラ 提交于 2019-12-12 15:42:23
问题 [sender LoadDataComplete:arrDetailData]; I am returning this array like this from an objective C class in my my class which inherits ViewController...if I write [arrDetailData release] below this...i get crash... Please advise on it...how to get this array without a leak 回答1: Why don't you autorelease it ? 回答2: @devaditya you should use autorelease instead of release [arrDetailData autorelease]; you can write this statement before the return statement. 回答3: when you are using [array relese]

Sorting an NSArray of NSDictionary

╄→гoц情女王★ 提交于 2019-12-12 15:24:03
问题 I have to sort an array of dictionaries but I have to order by an object in the dictionaries. 回答1: Use NSSortDescriptors with -sortedArrayUsingDescriptors:. For the key path, pass in the dictionary key, followed by the object's key(s) by which you want to sort. In the following example, you have an array of dictionaries and those dictionaries have a person under "personDictionaryKey", and the "person" has a "lastName" key. NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey

iphone sdk see size of local file (one created by application)

拥有回忆 提交于 2019-12-12 12:33:16
问题 Is there anyway I can look at the size of a file the application creates in bytes then store that number to use later? OR is there any way I can tell if an object in an NSArray is empty, I've tried everything, but it just doesn't work! 回答1: Use NSFileManager class' method - (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error The size key in the returned dictionary is defined as NSString * const NSFileSize; So for an example NSError *error = nil; NSDictionary

Why can you sometimes cast a NSArray to NSMutableArray, and sometimes you can't?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 10:59:27
问题 Specifically: self.words = (NSMutableArray*)[self.text componentsSeparatedByString:@" "]; works fine so long as there are separators. I see that the method returns the original string wrapped in an NSArray if there isn't, though. This single element NSArray stubbornly refuses to be cast as an NSMutableArray. But, when I do: self.words = [NSMutableArray arrayWithArray:self.words]; It works just fine. Is there something I'm missing here? Is it bad practice to cast from NSArray to NSMutableArray

NSArray and NSString

喜你入骨 提交于 2019-12-12 10:56:38
问题 The book I'm currently reading has me write the following code : -(IBAction)displaySomeText:(id)sender { NSString *cow = @"Milk"; NSString *chicken = @"Egg"; NSString *goat = @"Butter"; NSArray *food = [NSArray arrayWithObjects:cow, chicken, goat, nil]; NSString *string = @"The shopping list is: "; string = [string stringByAppendingString:[food componentsJoinedByString:@", "]]; [textView insertText:string]; } I understand somewhat how arrays work but I need help understanding the following

Is NSSet faster than NSArray?

≯℡__Kan透↙ 提交于 2019-12-12 09:13:48
问题 i am new to iOS. Anyone know if NSSet are faster than NSArray ? Why wouldn't I always just use an NSArray ? Anyone explain me difference between NSSet and NSArray ? 回答1: NSSet is used to have unique objects. NSArray may have duplicate objects. NSSet is an unordered collection. NSArray is an ordered collection. NSArray is faster than NSSet for simply holding and iterating. As little as 50% faster for constructing and as much as 500% faster for iterating. if you only need to iterate contents,

Comparing NSMutableArray Elements for Values

℡╲_俬逩灬. 提交于 2019-12-12 09:08:53
问题 I am looking for a way to compare the contents of two NSMutableArray objects. Both arrays are filled with NSMutableDictionaries which were allocated separately but occasionally contain the same data. Simplified Example: NSMutableArray *firstArray = [[NSMutableArray alloc] init]; NSMutableArray *secondArray = [[NSMutableArray alloc] init]; NSMutableDictionary *a = [[NSDictionary alloc] init]; [a setObject:@"foo" forKey:"name"]; [a setObject:[NSNumber numberWithInt:1] forKey:"number"];

Random results after parsing JSON in Objective-C

谁说我不能喝 提交于 2019-12-12 07:02:01
问题 I need to parse multiple JSON objects from several sources on a remote server and I need to populate UITableView with each value of the single parse: The single JSON source, example at h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A1: { "id": 0001, "main": { "mainA": 100, }, } The single JSON source, example at h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A2: { "id": 0002, "main": { "mainA": 200, }, } The single JSON source, example at h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A3: { "id": 0003,

Display a limited number of sorted annotations in Mapview

孤街浪徒 提交于 2019-12-12 06:12:41
问题 I have an NSArray of 500 annotations and I basically want to show only the ten nearest annotations to the user (the rest would not be added on Mapview) How should I do that? Here is my code: -(void) loadAndSortPOIs { [poiArray release]; nextPoiIndex = 0; NSString *poiPath = [[NSBundle mainBundle] pathForResource:@"annotations" ofType:@"plist"]; poiArray = [[NSMutableArray alloc] initWithContentsOfFile:poiPath]; CLLocation *homeLocation = [[CLLocation alloc] initWithLatitude:homeCoordinate