nsarray

Sort NSArray of NSDictionaries by string date in key?

泪湿孤枕 提交于 2019-11-30 20:45:29
I have been struggling to find an easy way to sort an NSMutableArray that contains NSDictionaries. Each NSDictionary has a key named "Date" which contains an NSString of a date (ex: 10/15/2014). What I am looking to do is sort the array based on those strings in ascending order. I have tried this with no luck: NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MM/dd/yy"]; NSDate *d1 = [formatter dateFromString:s1]; NSDate *d2 = [formatter dateFromString:s2]; return [d1 compare:d2]; //

Objective-c search locations with radius

本小妞迷上赌 提交于 2019-11-30 20:17:10
问题 Is there a library in for objective-c that will allow me to specify a radius and a location, and a list of locations and tell me which locations are within that radius? Thanks. 回答1: If you have CLLocations then something like this would work: // Given NSArray *locations as an array of CLLocation* that you wish to filter // and given a radius... CLLocationDistance radius = kSomeRadius; // and given a target you want to test against... CLLocation* target = [[CLLocation alloc] initWithLatitude

NSArray initialization methods

久未见 提交于 2019-11-30 18:24:35
问题 What is the difference between initializing array with NSArray * array = [NSArray array]; and NSArray * array = @[]; 回答1: NSArray * array = @[]; is the new way of doing NSArray * array = [NSArray array]; 回答2: @[] is shorthand for: id a = nil; NSArray* array = [NSArray arrayWithObjects:&a count:0]; Which is really just shorthand for [NSArray array] , for all intents and purposes. This is a feature added in a particular version of the compiler (and doesn't actually require runtime support for

Sorting Array received from Core Data in Swift

限于喜欢 提交于 2019-11-30 16:14:07
问题 I have a NSManagedObject my own class ( BPMonitor ) for my Entity - BPMonitor : import UIKit import CoreData @objc(BPMonitor) class BPMonitor: NSManagedObject { @NSManaged var sisPress: String @NSManaged var diaPress: String @NSManaged var hbPress: String @NSManaged var datePress: NSDate } In code I receive data from Core Data to array: var results:[BPMonitor]=[] ... ... How I can sort my array for field datePress ( NSDate ) in descending order? 回答1: You just need to use standard sort or

How do you change the elements within an NSArray?

南楼画角 提交于 2019-11-30 15:52:12
问题 I am a bit confused as to how arrays are handled in Objective-C. If I have an array such as NSarray *myArray = [[NSArray alloc] initWithObjects:@"N", @"N", @"N", @"N", @"N", nil]; how do I change the first occurrence to "Y"? 回答1: You need an NSMutableArray .. NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"N", @"N", @"N", @"N", @"N", nil]; and then [myArray replaceObjectAtIndex:0 withObject:@"Y"]; 回答2: You can't, because NSArray is immutable. But if you use NSMutableArray

Retrieve data from plist

拈花ヽ惹草 提交于 2019-11-30 15:30:02
问题 I have a plist and inside that an array and then set of dictionary elements? How can I retrieve data from the plist to my array? How can I get category names in one array? 回答1: Objective-C // Read plist from bundle and get Root Dictionary out of it NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]]; // Your dictionary contains an array of dictionary // Now pull an Array out of it. NSArray *arrayList = [NSArray

NSArray arrayWithObjects: if nil is meant to mark array end, can I do …nil, nil]?

主宰稳场 提交于 2019-11-30 15:28:14
If nil is meant to mark the end of parameters, then can I use: [NSArray arrayWithObjects:obj1, obj2, nil, nil, nil]; as the first nil marks array end and two nils after will be ignored? I got two opposite answers and will try it out. update: The reason why I need to do this, is I need to create an UIAlertView, which may have buttons: 'OK' only, or 'Call' and 'Cancel', so either 'OK' or 'Cancel', is the cancel button, whereas 'Call' is other buttons in one case, in other cases, I do not need any other buttons, that's why I need to do this. The addition of nil to the end is not intended to add

Print array in objective-c?

淺唱寂寞╮ 提交于 2019-11-30 14:45:07
问题 I need to check my values from NSArray which are stored dynamically. How should I print the array values in Objective-C? 回答1: NSLog(@"%@",yourArray); This actually calls the description method of your NSArray and prints it to the log. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/description 来源: https://stackoverflow.com/questions/3396336/print-array-in-objective-c

CLGeocoder Only Returning One Placemark

蓝咒 提交于 2019-11-30 13:22:46
问题 I have a problem with CLGeocoder where when I call geocodeAddressString:withCompletionHandler I only ever get one result back, despite knowing that the inputted string should return more than one value. The class reference even states: In the case of forward-geocoding requests, multiple placemark objects may be returned if the provided information yielded multiple possible locations. However, my placemarks array only ever has one item in it: [geocoderDestination geocodeAddressString

Retrieve NSDictionary from NSArray where dictionary key has value X

左心房为你撑大大i 提交于 2019-11-30 12:57:13
I have an NSArray with NSDictionaries . One of the dictionaries keys in one of the arrays contains a value. I want to retrieve the NSDictionary with that value. My array: Array: ( { DisplayName = "level"; InternalName = "Number 2"; NumberValue = 1; }, { DisplayName = "PurchaseAmount"; InternalName = "Number 1"; NumberValue = 3500; } ) So, I would like to get the dictionary which contains DisplayName set to PurchaseAmount (case insensitive). How can I accomplish that? LIKE[cd] will also do it NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(DisplayName