nsarray

objective-c nsarray to c array

不打扰是莪最后的温柔 提交于 2019-12-07 09:46:23
问题 Sorry, I'm not even sure how to ask, since I'm a complete newbie at C, pointers and stuff like that. There's a function that accepts an argument: char **arg . If I write that argument like so: char *cargs[] = {"blah", NULL}; and pass it to the function: function(cargs); it works. but ... I have an NSArray of NSStrings and I need to make this array out of values from NSArray . I figured it should be a matter of creating a C array of the same element count as NSArray and copy the strings,

Save Array to plist

不打扰是莪最后的温柔 提交于 2019-12-07 09:23:02
问题 I'm trying to store some items into a pList . Here is the array loop: for (id obj in items) NSLog(@"obj: %@", obj); Output NSLog: 2013-03-27 13:00:40.072 mycode[47436:c07] obj: Red 2013-03-27 13:00:40.073 mycode[47436:c07] obj: Blue 2013-03-27 13:00:40.073 mycode[47436:c07] obj: Green // The arrayWithObjects works. But I don't know how to (loop?) through my items for saving into the plist file... NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

Method swizzling for NSArray

梦想与她 提交于 2019-12-07 07:05:07
问题 I'm trying to debug something on an NSArray and I can't even find what the pointer to the array that's causing the issue is and I have no idea why it's happening. I'm getting an error on objectAtIndex: (out of bounds) and it seems to be coming from some internal NSView method... anyway, I tried swizzling objectAtIndex: with my own, but it won't work. What's strange is I can do the same thing but with another class and method, and it works fine. Here's what I'm doing to swizzle: Class

Find duplicates in NSArray

≡放荡痞女 提交于 2019-12-07 06:45:08
问题 Say you have an NSArray with duplicates @[1,2,3,1,1,2,4,5,6] ; Find all the duplicates; this can be in pseudocode. This is more of a algorithm question than a Foundation framework (without the use of NSSet ) question. 回答1: as @Lithu described, use NSCountedSet , see the below code. NSArray *arr = [[NSArray alloc]initWithObjects:@(1),@(1),@(2), @(1),nil]; NSCountedSet *cs = [[NSCountedSet alloc] initWithArray:arr]; NSLog(@"object count greater than 1 are"); for(NSNumber *num in cs) { if([cs

Objective C - Subclassing NSArray

心不动则不痛 提交于 2019-12-07 06:30:41
问题 I am trying to subclass NSArray , but it crashes the app when trying to access the count method. I know that NSArray is a class cluster . But what does this mean? Is there a work around to be able to subclass an NSArray? I know that I can simply subclass NSObject and have my array as an instance variable but I would rather subclass NSArray . EDIT: Reason: I am creating a card game, I have a class Deck which should subclass NSMutableArray to have a couple of extra methods ( -shuffle ,

NSString from NSArray

梦想与她 提交于 2019-12-07 06:02:59
问题 I am trying to create a String from Array.But, there is condition to how it should be generated, as explained below. NSArray *array=[NSArray arrayWithObjects:@"Hello",@"World",nil]; [array componentsJoinedByString:@","]; This will output: Hello,World. But, if first Item is Empty,then is there way to receive the only second one. Hello , @"" => Hello @"" , World => World Hello , World => Hello,World 回答1: Another way to do this is to grab a mutable copy of the array and just remove non valid

How to convert a JSON String into an NSArray? [closed]

五迷三道 提交于 2019-12-07 05:55:31
问题 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 6 years ago . I am facing an issue while converting an NSString to NSArray. My string is : ["Default", "Discipleship", "Faith", "Family", "Hope", "Life Building", "Love", "Missions", "Relationships"] What i want to do is get the elements(Default,Discipleship etc.) out of this string and put them into an NSArray. I have tried

How can I convert NSMutableArray into NSString?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 05:04:02
问题 I am trying to convert (or copy?) a NSMutableArray into a NSString . I guess my problem is that I don't really understand the structure of a NSString . After conversion I want to attached it in email body. Here is my code: - (IBAction)sendEmail { NSLog(@"sendEmail"); [textView resignFirstResponder]; [textView1 resignFirstResponder]; if ([MFMailComposeViewController canSendMail]) { // set the sendTo address NSMutableArray *recipients = [[NSMutableArray alloc] initWithCapacity:1]; [recipients

How to initialize NSArrays / NSMutableArrays when declared as properties

倾然丶 夕夏残阳落幕 提交于 2019-12-07 03:01:05
问题 I need some help in understanding how to use NSArrays / NSMutableArrays as properties. What property attributes should the arrays be : strong or copy ? In which case should I use which attribute? How do I initialize the arrays in code. Should my property array be NSArray or NSMutableArray Currently the way I am doing it 2 different ways as below. But it is all patchy and done with no clear understanding of the mechanics of it. Which one of it is correct or incorrect. Approach 1 .m file

Accessing NSArray's items with subscript

喜欢而已 提交于 2019-12-07 01:38:40
问题 Is it possible to access NSArray's objects using [idx]? I have a standard library that uses [] style indexing and I don't want to rewrite the whole library to suit ObjC's objectAtIndex: method. As in, NSArray *obj = [NSArray ...]; id item = obj[0]; 回答1: The accepted answer (whilst true at the time) is now out of date. As of Xcode 4.5, you can now set and get NSArray elements using: id object = array[5]; // equivalent to [array objectAtIndex:5]; mutableArray[5] = object; // equivalent to