nsarray

How to search an NSSet or NSArray for an object which has an specific value for an specific property?

孤人 提交于 2019-11-27 07:22:39
How to search an NSSet or NSArray for an object which has an specific value for an specific property? Example: I have an NSSet with 20 objects, and every object has an type property. I want to get the first object which has [theObject.type isEqualToString:@"standard"] . I remember that it was possible to use predicates somehow for this kind of stuff, right? Ole Begemann NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == %@", @"standard"]; NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate]; id firstFoundObject = nil; firstFoundObject = filteredArray.count

Pass a NSDictionary as parameter to UITapGestureRecognizer

对着背影说爱祢 提交于 2019-11-27 07:13:35
问题 I want to pass a NSArray as a parameter to UITapGestureRecognizer and access it in downloadOptionPressed method. How can I do this ? The NSArray NSArray *parameters = [NSArray arrayWithObjects:currentTrack, nil]; Creating the UITapGestureRecognizer UITapGestureRecognizer *downloadOptionPressed = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeFrameLabelTapped:)]; [downloadOption addGestureRecognizer:downloadOptionPressed]; The downloadOptionPressed method -(void

Maximum amount of objects in NSArray

徘徊边缘 提交于 2019-11-27 06:54:49
问题 What is the largest amount of objects I can put in my NSArray? 回答1: The NSArray initWithCapacity method, takes an unsigned int as an argument. So whatever the maximum value of an unsigned int is on your platform may be the theoretical limit. However the actual limit is more likely to depend on the amount of memory you have available. 回答2: Have you tried to find out? ;) NSMutableArray * a = [[NSMutableArray alloc] init]; NSUInteger i = 0; @try { while (1) { [a addObject:@"hi"]; i++; } } @catch

Multi-dimensional NSArray object

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:48:35
问题 Is there a way to create two dimensional NSArray without nesting arrays in the primitive format aFloatArray[][]. Thank you. 回答1: Unfortunately not. To create a multi-dimensional NSArray: NSArray *multiArray = [NSArray arrayWithObjects: [NSMutableArray array], [NSMutableArray array], [NSMutableArray array], [NSMutableArray array], nil]; // Add a value [[multiArray objectAtIndex:1] addObject:@"foo"]; // get the value NSString *value = [[multiArray objectAtIndex:1] objectAtIndex:0]; However, you

Check that the contents of one NSArray are all in another array

最后都变了- 提交于 2019-11-27 06:20:16
问题 I have one NSArray with names in string objects like this: @[@"john", @"smith", @"alex", @"louis"] , and I have another array that contains lots of names. How can I check that all the objects in the first array are in the second? 回答1: NSSet has the functionality that you are looking for. If we disregard performance issues for a moment, then the following snippet will do what you need in a single line of code: BOOL isSubset = [[NSSet setWithArray: array1] isSubsetOfSet: [NSSet setWithArray:

Better way to convert NSArray of NSNumbers to array of NSStrings

孤者浪人 提交于 2019-11-27 05:57:20
问题 I have an NSArray consisting of NSNumbers and I want to convert this to an NSArray of NSStrings , by getting the stringValue of each NSNumber in the first array. The method that comes to my mind is iterating each value in the first one, getting its string value and adding it into another array. But there should be a more elegant solution for this. Do you know one? 回答1: NSArray implements the Key-Value Coding method valueForKey: in such a way that it returns a new array. The new array contains

Shuffling an array in objective-c [duplicate]

旧巷老猫 提交于 2019-11-27 05:46:05
问题 Possible Duplicate: What’s the Best Way to Shuffle an NSMutableArray? I develop apps for iphone/iPad.I want to shuffle the objects stored in an NSArray.Is there any way to achieve it with objective-c? 回答1: Add a category to NSMutableArray, with code provided by Kristopher Johnson - // NSMutableArray_Shuffling.h #if TARGET_OS_IPHONE #import <UIKit/UIKit.h> #else #include <Cocoa/Cocoa.h> #endif // This category enhances NSMutableArray by providing // methods to randomly shuffle the elements.

Check if array contains part of a string in Swift?

半城伤御伤魂 提交于 2019-11-27 05:33:47
问题 I have an array containing a number of strings. I have used contains() (see below) to check if a certain string exists in the array however I would like to check if part of a string is in the array? itemsArray = ["Google, Goodbye, Go, Hello"] searchToSearch = "go" if contains(itemsArray, stringToSearch) { NSLog("Term Exists") } else { NSLog("Can't find term") } The above code simply checks if a value is present within the array in its entirety however I would like to find "Google, Google and

Array of tuples in Swift

回眸只為那壹抹淺笑 提交于 2019-11-27 05:16:33
I have a function: func parseJSON3(inputData: NSData) -> NSArray { var tempDict: (id:Int, ccomments:Int, post_date:String, post_title:String, url:String) = (id: 0, ccomments: 0, post_date: "null", post_title: "null", url: "null") var resultArray: (id:Int, ccomments:Int, post_date:String, post_title:String, url:String)[] = [] var error: NSError? var jsonDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary var firstArray = jsonDictionary.objectForKey("locations") as NSArray for dict in

Using NSRegularExpression to extract URLs on the iPhone

倖福魔咒の 提交于 2019-11-27 04:39:47
I'm using the following code on my iPhone app, taken from here to extract all URLs from striped .html code. I'm only being able to extract the first URL, but I need an array containing all URLs. My NSArray isn't returning NSStrings for each URL, but the objects descriptions only. How do I make my arrayOfAllMatches return all URLs, as NSStrings? -(NSArray *)stripOutHttp:(NSString *)httpLine { // Setup an NSError object to catch any failures NSError *error = NULL; // create the NSRegularExpression object and initialize it with a pattern // the pattern will match any http or https url, with