nsmutablearray

Cocoa structs and NSMutableArray

ぐ巨炮叔叔 提交于 2019-12-03 05:59:17
问题 I have an NSMutableArray that I'm trying to store and access some structs. How do I do this? 'addObject' gives me an error saying "Incompatible type for argument 1 of addObject". Here is an example ('in' is a NSFileHandle, 'array' is the NSMutableArray): //Write points for(int i=0; i<5; i++) { struct Point p; buff = [in readDataOfLength:1]; [buff getBytes:&(p.x) length:sizeof(p.x)]; [array addObject:p]; } //Read points for(int i=0; i<5; i++) { struct Point p = [array objectAtIndex:i]; NSLog(@

How to exchange elements in swift array?

拈花ヽ惹草 提交于 2019-12-03 04:17:39
I have one simple array like: var cellOrder = [1,2,3,4] I want to exchange elements like suppose a second element with first element. And result will be: [2,1,3,4] I know we can use exchangeObjectAtIndex with NSMutableArray But I want to use swift array. Any ways to do same with swift [Int] array? Use swap : var cellOrder = [1,2,3,4] swap(&cellOrder[0], &cellOrder[1]) Alternately, you can just assign it as a tuple: (cellOrder[0], cellOrder[1]) = (cellOrder[1], cellOrder[0]) hstdt Swift 4 swapAt(_:_:) : cellOrder.swapAt(index0, index1) One option is: cellOrder[0...1] = [cellOrder[1], cellOrder

Total Size of NSMutableArray object

杀马特。学长 韩版系。学妹 提交于 2019-12-03 04:15:30
I've got an NSMutableArray that holds a bunch of objects, what I'm trying to figure out is how much memory is the array using. After looking at a couple of places I know about the size of call, and when I make it I get 32 bits (which is the size of the NSMutableArray object it self). Example code: NSMutableArray *temp = [[NSMutableArray alloc]init]; [temp addObject:objectxyz]; [temp addObject:objectabc]; [temp addObject:object123]; now I want to know the size :) To get the number of objects in the array, use [temp count] If you want the total memory usage of the array, you'll have to loop

Difference between NSArray and NSMutableArray

那年仲夏 提交于 2019-12-03 03:51:20
问题 What is the difference b/w NSArray and NSMutableArray ? 回答1: NSMutableArray (and all other classes with Mutable in the name) can be modified. So, if you create a plain NSArray , you cannot change its contents later (without recreating it). But if you create an NSMutableArray , you can change it — you'll notice it has methods like -addObject: and -insertObject:atIndex: . See the documentation for details. 回答2: The "mutable" types are classes which can be changed after they've been initialized,

Converting NSArray to NSMutableArray [closed]

笑着哭i 提交于 2019-12-03 03:38:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . As above. Would be helpful to know. Thanks! 回答1: Here are two options: - (NSMutableArray *)createMutableArray1:(NSArray *)array { return [NSMutableArray arrayWithArray:array]; } - (NSMutableArray *)createMutableArray2:(NSArray *)array { return [[array mutableCopy] autorelease]; } 回答2: Use -mutableCopy and make

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

时间秒杀一切 提交于 2019-12-03 03:14:03
问题 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]

Searching a NSArray for the nearest number(s)

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there an easy way to search an NSArray of numbers to find the nearest (or exact if it exists) matches to a user-input number? Say I have an array like this: 7, 23, 4, 11, 18, 2 , and the user enters 5 . The program returns the three nearest values in descending order of closeness: 4, 7, 2 , and most importantly gives the NSArray index of the three objects: 2, 0, 5 . 回答1: Update: see below for a better solution than my first one. Here's a solution using NSDictionary wrappers for each number and its index, with sorting using a comparator

Objective-C Simplest way to create comma separated string from an array of objects

↘锁芯ラ 提交于 2019-12-03 02:55:27
问题 So I have a nsmutablearray with a bunch of objects in it. I want to create a comma separated string of the id value of each object. 回答1: Use the NSArray instance method componentsJoinedByString: . In Objective-C: - (NSString *)componentsJoinedByString:(NSString *)separator In Swift: func componentsJoinedByString(separator: String) -> String Example: In Objective-C: NSString *joinedComponents = [array componentsJoinedByString:@","]; In Swift: let joinedComponents = array.joined(seperator: ",")

Attempt to insert non-property value Objective C

这一生的挚爱 提交于 2019-12-03 02:50:42
问题 Hi l am trying to create a fourates lists from an restaurants Object, my application has a list of different restaurants, l want the ability for users to add favourate restaurants, and this code is not working - (IBAction)toggleFav:(id)sender { Restaurant *resto = [self restaure]; NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; [dic setObject:resto.price forKey:@"restoPrice"]; [dic setObject:resto.restaurantId forKey:@"restaurantId"]; [dic setObject:resto.restoAbout forKey:@

Flatten an NSArray

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an array like this: array: ( ( "http://aaa/product/8_1371121323.png", "http://aaa/product/14_1371123271.png" ), ( "http://aaa/product/9_1371121377.png" ) ) and I have to create another array from that one like this array: ( "http://aaa/product/8_1371121323.png", "http://aaa/product/14_1371123271.png", "http://aaa/product/9_1371121377.png" ) How can I do that? Is it possible to combine all the objects and separate them using some string? 回答1: Sample Code : NSMutableArray *mainArray = [[NSMutableArray alloc] init]; for (int i = 0; i 回答2