nsarray

Executing Blocks From NSArray?

纵饮孤独 提交于 2019-12-03 03:31:47
问题 I was just thinking, as you can treat Blocks like objects if I create two of them and then add them to an NSArray is there a way to execute them from the array? int (^Block_001)(void) = ^{ return 101; }; int (^Block_002)(void) = ^{ return 202; }; NSArray *array = [NSArray arrayWithObjects:Block_001, Block_002, nil]; EDIT: Update for clarity Per @davedelong's excellent answer int (^Block_001)(void) = [^{ return 101; } copy]; int (^Block_002)(void) = [^{ return 202; } copy]; NSArray *array =

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]

Beautify NSLog of NSArray & NSDictionary

≡放荡痞女 提交于 2019-12-03 03:01:49
问题 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 :) 回答1: 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

Loop through NSDictionary to create separate NSArrays

a 夏天 提交于 2019-12-03 02:34:49
I have a large NSDictionary that I need to loop through and create separate NSArray s. Here are the contents: ( { id = { text = ""; }; sub = { text = " , "; }; text = ""; "thumb_url" = { text = ""; }; title = { text = "2010-2011"; }; type = { text = "title"; }; }, { id = { text = "76773"; }; sub = { text = "December 13, 2010"; }; text = ""; "thumb_url" = { text = "http://www.puc.edu/__data/assets/image/0004/76774/varieties/thumb.jpg"; }; title = { text = "College Days - Fall 2010"; }; type = { text = "gallery"; }; }, { id = { text = ""; }; sub = { text = ""; }; text = ""; "thumb_url" = { text

How to separate string by space using Objective-C?

主宰稳场 提交于 2019-12-03 02:34:38
问题 Assume that I have a String like this: hello world this may have lots of sp:ace or little space I would like to seperate this String to this: @"hello", @"world", @"this", @"may", @"have", @"lots", @"of", @"sp:ace", @"or", @"little", @"space" Thank you. 回答1: NSString *aString = @"hello world this may have lots of sp:ace or little space"; NSArray *array = [aString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; array = [array filteredArrayUsingPredicate:

How to return an NSMutableArray from an NSSet

安稳与你 提交于 2019-12-03 02:30:59
问题 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? 回答1: 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

Split an NSString into an array in Objective-C

≯℡__Kan透↙ 提交于 2019-12-03 02:06:45
How can I split the string @"Hello" to either: a C array of 'H' , 'e' , 'l' , 'l' , 'o' or: an Objective-C array of @[@"H", @"e", @"l", @"l", @"o"] If you're satisfied with a C array of char s, try: const char *array = [@"Hello" UTF8String]; If you need an NSArray, try: NSMutableArray *array = [NSMutableArray array]; NSString *str = @"Hello"; for (int i = 0; i < [str length]; i++) { NSString *ch = [str substringWithRange:NSMakeRange(i, 1)]; [array addObject:ch]; } And array will contain each character as an element of it. Try this : - (void) testCode { NSString *tempDigit = @"12345abcd" ;

Creating an array from properties of objects in another array

限于喜欢 提交于 2019-12-03 00:45:46
问题 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). 回答1: This will return an

Create a Custom Formatted Dictionary Array from CSV File of Data

ぐ巨炮叔叔 提交于 2019-12-02 23:39:29
问题 I've created an iPhone app that has a dictionary array of locations (lat,long,point). I created the array by manually entering each value. myLocationArray = @[ @{ kStation : @"1", kLatitude : @( 41.656467), kLongitude : @(-81.277963) }, @{ kStation : @"2", kLatitude : @(41.657118), kLongitude : @(-81.276545) }, @{ kStation : @"3", kLatitude : @(41.658493), kLongitude : @(-81.273542) }, ... This is good and works but now I want to create this array programmatically by getting the data from a

NSDictionary sort by keys as floats

孤街浪徒 提交于 2019-12-02 22:20:43
basically I have an NSDictionary with keys and values. The keys are all numbers, but at the moment they are strings. I want to be able to compare them as numbers in order to sort them. eg: If I have a Dictionary like this: { "100" => (id)object, "20" => (id)object, "10" => (id)object, "1000" => (id)object, } I want to be able to sort it like this: { "10" => (id)object, "20" => (id)object, "100" => (id)object, "1000" => (id)object, } Any ideas? Thanks Tom Not sure what you are up to – dictionaries are inherently unsorted, there is no stable key ordering in the default implementation. If you