nsarray

Get n random objects (for example 4) from nsarray

空扰寡人 提交于 2019-12-18 11:54:24
问题 I have a large NSArray of names, I need to get random 4 records (names) from that array, how can I do that? 回答1: #include <stdlib.h> NSArray* names = ...; NSMutableArray* pickedNames = [NSMutableArray new]; int remaining = 4; if (names.count >= remaining) { while (remaining > 0) { id name = names[arc4random_uniform(names.count)]; if (![pickedNames containsObject:name]) { [pickedNames addObject:name]; remaining--; } } } 回答2: I made a caregory called NSArray+RandomSelection . Just import this

Is there an easy way to iterate over an NSArray backwards?

天大地大妈咪最大 提交于 2019-12-18 11:39:42
问题 I've got an NSArray and have to iterate over it in a special case backwards, so that I first look at the last element. It's for performance reasons: If the last one just makes no sense, all previous ones can be ignored. So I'd like to break the loop. But that won't work if I iterate forward from 0 to n. I need to go from n to 0. Maybe there is a method or function I don't know about, so I wouldn't have to re-invent the wheel here. 回答1: To add on the other answers, you can use -[NSArray

transactionReceipt for in-app purchase is deprecated in iOS 7. What can I replace it with?

▼魔方 西西 提交于 2019-12-18 11:05:16
问题 In iOS 7, on the SKPaymentTransaction class, the property transactionReceipt : // Only valid if state is SKPaymentTransactionStatePurchased. @property(nonatomic, readonly) NSData *transactionReceipt …is deprecated. But, in my code, I created a InAppPurchase class, and in my method for controlling how is the method buying, I'm using the delegate method in my code and it's like: - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { for (SKPaymentTransaction

Filtering NSArray of NSDictionary objects using NSPredicate

删除回忆录丶 提交于 2019-12-18 10:31:51
问题 I have an NSArray of NSDictionary objects. I want to filter the array based on keys of the dictionaries using NSPredicate. I have been doing something like this: NSString *predicateString = [NSString stringWithFormat:@"%@ == '%@'", key, value]; NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString]; NSArray *filteredResults = [allResultsArray filteredArrayUsingPredicate:predicate]; This works fine if they key passed in is one-word: Color, Name, Age. But it doesn't work if

How can I check if an object in an NSArray is NSNull?

≡放荡痞女 提交于 2019-12-18 10:25:09
问题 I am getting an array with null value. Please check the structure of my array below: ( "< null>" ) When I'm trying to access index 0 its crashing because of -[NSNull isEqualToString:]: unrecognized selector sent to instance 0x389cea70 Currently its crashing because of that array with a crash log: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x389cea70' *** First throw call stack:

Grouping NSArray of NSDictionary based on a key in NSDictionay

若如初见. 提交于 2019-12-18 06:06:25
问题 I am trying to filter out a NSArray of NSDictionaries. With my below example, I want dict1, dict2 & dict4 grouped in one array, dict3 & dict5 grouped in second array and dict6 in third array. I am getting this data in NSArray, so essentially the "orig" array below is my input and I know that I need to do grouping based on "Name" key. Instead of looping through the NSArray I though of using valueForKeyPath to return me array based on the key path but this does not work (crashes with logs -

makeObjectsPerformSelector:

Deadly 提交于 2019-12-18 02:37:35
问题 I want to make all objects in an array perform a selector. I've discovered the appropriately named makeObjectsPerformSelector: method, but I have a question with it. If I use it on an array, will it change the existing array or return a new one? If it modifies the existing object, what be the easiest way to return a new array with the selector applied? 回答1: makeObjectsPerformSelector: is going to run that selector against every object in the array. If those objects are modified by the

Static NSArray of strings - how/where to initialize in a View Controller

那年仲夏 提交于 2019-12-17 23:20:57
问题 In a Master-Detail app I'd like to display a TableView with 5 sections titled: Your Move Their Move Won Games Lost Games Options So I create a blank Master-Detail app in Xcode 5.0.2 and then in its MasterViewController.m (which is a UITableViewController) I'm trying to implement the method: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return _titles[section]; } My question is however how to init the NSArray _titles? I'm trying in the

How to stop enumerateObjectsUsingBlock Swift

亡梦爱人 提交于 2019-12-17 22:57:51
问题 How do I stop a block enumeration? myArray.enumerateObjectsUsingBlock( { object, index, stop in //how do I stop the enumeration in here?? }) I know in obj-c you do this: [myArray enumerateObjectsUsingBlock:^(id *myObject, NSUInteger idx, BOOL *stop) { *stop = YES; }]; 回答1: In Swift 1: stop.withUnsafePointer { p in p.memory = true } In Swift 2: stop.memory = true In Swift 3 - 4: stop.pointee = true 回答2: This has unfortunately changed every major version of Swift. Here's a breakdown: Swift 1

Apply vertical alpha gradient to UITableView

喜欢而已 提交于 2019-12-17 22:56:02
问题 I'm new to iOS development and am trying to learn Swift. I'd like to apply a vertical alpha gradient to a UITableView, but am having some trouble. Originally following this SO post, I did the following: var gradientMaskLayer:CAGradientLayer = CAGradientLayer() gradientMaskLayer.frame = myTableView.bounds gradientMaskLayer.colors = [UIColor.clearColor().CGColor, UIColor.blackColor().CGColor] gradientMaskLayer.locations = [0.0, 0.05] myTableView.layer.mask = gradientMaskLayer After getting the