nsmutablearray

Semaphore回顾

孤街浪徒 提交于 2019-11-28 05:03:53
用途 多线程访问可变变量时,是非线程安全的。可能导致程序崩溃。此时,可以通过使用信号量(semaphore)技术,保证多线程处理某段代码时,后面线程等待前面线程执行,保证了多线程的安全性。使用方法记两个就行了,一个是wait(dispatch_semaphore_wait),一个是signal(dispatch_semaphore_signal)。根据dispatch_semaphore_wait的参数类型提示去创建信号量(dispatch_semaphore_t)即可。 感觉原理跟PV操作如出一辙。 不用semaphore时code: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); NSMutableArray *lArrM = [NSMutableArray array]; for (int i = 0; i < 100; ++i) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ [lArrM addObject:@(i)]; }); } } return 0; } 运行结果

How can I sort an NSMutableArray alphabetically?

心已入冬 提交于 2019-11-28 05:01:27
I want to sort an NSMutableArray alphabetically. You can do this to sort NSMutableArray: [yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; The other answers provided here mention using @selector(localizedCaseInsensitiveCompare:) This works great for an array of NSString, however the OP commented that the array contains objects and that sorting should be done according to object.name property. In this case you should do this: NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; [yourArray sortUsingDescriptors:[NSArray arrayWithObject

Saving an NSMutableArray to Core Data

半城伤御伤魂 提交于 2019-11-28 04:44:40
I want to add an NSMutableArray of NSStrings to one of my Entities in my core data model. The problem is that this isn't a supported type in Core Data. I tried making a tranformable attribute, but the problem is that I see no way of turning a NSMutableArray to NSData, and then going from NSData, back to an NSMutableArray. Does anyone have an idea as to how this issue can be solved? (I know I can archive the array, but I don't want to do that, I want it to be present in my model). jbrennan You could have a binary data attribute in your modeled object, archive the array to data, and hand it off

how and where do I initialize an global NSMutableArray in Xcode 5

时光怂恿深爱的人放手 提交于 2019-11-28 04:13:31
问题 I am trying to initialize a global NSMutableArray that I can add integers to later. I just need to know how and where I should initialize my array so that it can be accessed and changed by any function that I use later in my program. Also I am using Xcode 5 and know that the array needs to be 180 in length. 回答1: In your AppDelegate.h file - @property(nonatomic,retain) NSMutableArray *sharedArray; In AppDelegate.m @synthesize sharedArray; In didFinishLaunchingWithOptions - sharedArray = [

How do you store data from NSMutable Array in Core Data?

人盡茶涼 提交于 2019-11-28 04:05:44
问题 The app i'm making draws a Polyline based on the users coordinates from CLLocationManager (these are all kept in an NSMutableArray ) When the app closes, the current polyline disappears. How can I store the array of points in CoreData to be retrieved when the app starts up? All of the past 'routes' that the user has taken since initiating the app should be recovered and overlaid on the map (which may be quite a lot, so CoreData seems the best option from what i've gathered). This is the code

UIActivityViewController - Email and Twitter sharing

别来无恙 提交于 2019-11-28 03:28:43
I recently started working with UIActivity to share my app to the world, but I have few problems. First, I didn't find how to set the subject of my email. Is there any way? Second, when I set the body text of my email, there is a extra "enter" (first line of the email is blank and my text starts at the second line). Here's the code: NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Test", nil]; UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil]; And in the email, it shows that: " Test "

NSMutableArray Data Attachement With E-mail Body?

China☆狼群 提交于 2019-11-28 02:20:33
问题 My NSMutableArray data are in NSData formate.I am trying to attached NSMutableArray data to E-mail body.Here is my NSMutableArray code: NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults]; NSString *msg1 = [defaults1 objectForKey:@"key5"]; NSData *colorData = [defaults1 objectForKey:@"key6"]; UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData]; NSData *colorData1 = [defaults1 objectForKey:@"key7"]; UIColor *color1 = [NSKeyedUnarchiver unarchiveObjectWithData

Trying to populate NSMutableArray but get implicit conversion of int to id is disallowed with arc

╄→гoц情女王★ 提交于 2019-11-28 02:03:06
问题 I am simply trying to add a series of numbers to an NSMutableArray but I am getting the error: implicit conversion of int to id is disallowed with arc resultArray = [[NSMutableArray alloc]init]; for (int i = 0; i <= 9; i++) { [resultArray addObject:i]; } What am I doing wrong? 回答1: An NSArray or NSMutableArray can only hold objects, not primitive data types such as int s. So if you want to add them to the array, you'll have to wrap them in an NSNumber . You can do the following: [resultArray

How to add properties to NSMutableArray via category extension?

给你一囗甜甜゛ 提交于 2019-11-28 01:21:29
I know that I can "subclass" an NSMutableArray using "category extensions," i.e. @interface NSMutableArray (MyExtension) , to add new functions to the class. However, is there a way using category extensions to also add new properties to the extension? Note that @interface NSMutableArray (MyStuff) is a category and not a class extension . They are similar in this context, but actually do have quite a few different details. You can't add storage to an existing class through either mechanism. You can use Associative References to associate data with an instance, though. As you said, categories

How to Sort an NSMutableArray of Managed Objects through an object graph

风流意气都作罢 提交于 2019-11-27 23:37:00
问题 I'm new to Apple's iPhone 3.0 SDK support of Core Data and I'm relatively new to iPhone development. My question is on slightly more "complex" sorting than is covered in the docs. I suspect that I am making an incorrect, fundamental assumption. How can I sort a pre-fetched collection of managed objects by a property contained several layers deeper in the collection? Example: Farm has a 1 to many to Cowboy (Farm.cowboys) Cowboy has a 1 to 1 to Person (Cowboy.person) Person has lastName (Person