nsarray

Objective-C how to pull several random items out of NSArray without getting duplicates? [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-23 02:49:38
问题 This question already has answers here : Getting a random object from NSArray without duplication (3 answers) Closed 3 years ago . I have an array of random properties I would like to assign to equipment within the game I'm developing. The code that I use below is returning an NSArray. I'm interested if there's way to get item indices from that array without getting duplicate values. The obvious solution is to create a mutable array with the returned array, do random, remove item that was

NSArray Loop to grab Objects

不羁的心 提交于 2019-12-22 12:22:17
问题 I'm currently in the development process of an application which needs to be able to grab two objects from an NSArray and then store it in another object. I've currently got this fast enumeration loop going on, NSUInteger count = 0; NSUInteger i = count + 1; for (id item in [section items]) { item1 = [section.items objectAtIndex:count]; item2 = [section.items objectAtIndex:i]; count++; } Now, what I want to do is grab the object in the first position and store in item1, and then the second

Add objects to an Array from another View Controller

巧了我就是萌 提交于 2019-12-22 09:28:50
问题 I have a HomeViewController which has a tableView populated with the array tableViewArray (originally empty). When I tap on a barButton, I segue modally to another View Controller called OutsideViewController which has another tableView populated by a different array. What I would like to do is the following: When I tap on a row in my OutsideViewController , I would like to add the selected string value to the tableViewArray so that when I go back to HomeViewController , the tableView has

what is the use of @[ ] in objective c [duplicate]

一曲冷凌霜 提交于 2019-12-22 09:20:13
问题 This question already has answers here : What are the details of “Objective-C Literals” mentioned in the Xcode 4.4 release notes? (3 answers) Closed 6 years ago . I have seen NSArray *objectsToShare = @[objects]; when looking at some example code. What is the meaning of @[objects] here ? 回答1: NSArray *objectsToShare = @[objects]; is the same as NSArray *objectsToShare = [NSArray arrayWithObjects:objects count:count]; 回答2: It is also known as Literals in Objective-C Examples Immutable array

Finding most repeated object in array

╄→гoц情女王★ 提交于 2019-12-22 06:46:52
问题 I have an array full of strings with each string being a name. Some names may be the same while some may be different. The language I'm working in is objective-C. I want to be able to find out which name is most popular from this array (the array will be dynamic based on information given to the app from the user). I am not sure how to make this happen EFFICIENTLY. If someone could expand on this or provide an example, it would be appreciated. Thank you Example: NSArray *nameArray= [[NSArray

How do I extend an NSArray?

孤街浪徒 提交于 2019-12-22 05:19:25
问题 Here's my try: H file: @interface Strings : NSArray @end M file: @implementation Strings - (id) init { [self initWithObjects: @"One.", nil]; return self; } @end When I run I get this: 'NSInvalidArgumentException', reason: ' * -[NSArray initWithObjects:count:]: method only defined for abstract class. Define -[Strings initWithObjects:count:]!' This is what I did instead: H file: @interface Strings : NSObject + (NSArray*) getStrings; @end M file: @implementation Strings + (NSArray*) getStrings {

NSArray out of bounds check

Deadly 提交于 2019-12-22 01:39:17
问题 noobie question.. What is the best way to check if the index of an NSArray or NSMutableArray exists. I search everywhere to no avail!! This is what I have tried: if (sections = [arr objectAtIndex:4]) { /*.....*/ } or sections = [arr objectAtIndex:4] if (sections == nil) { /*.....*/ } but both throws an "out of bounds" error not allowing me to continue (do not reply with a try catch because thats not a solution for me) Thanks in advance 回答1: if (array.count > 4) { sections = [array

NSArray out of bounds check

旧时模样 提交于 2019-12-22 01:36:20
问题 noobie question.. What is the best way to check if the index of an NSArray or NSMutableArray exists. I search everywhere to no avail!! This is what I have tried: if (sections = [arr objectAtIndex:4]) { /*.....*/ } or sections = [arr objectAtIndex:4] if (sections == nil) { /*.....*/ } but both throws an "out of bounds" error not allowing me to continue (do not reply with a try catch because thats not a solution for me) Thanks in advance 回答1: if (array.count > 4) { sections = [array

How to use NSDate formatter with an array of dates?

☆樱花仙子☆ 提交于 2019-12-22 01:07:40
问题 I'm trying to generate an array of 30 days from the current date and put them into an array formatted correctly. I currently have a working generater to get 30 days from the current date and put it into an array but I don't know how to use NSDate formatter for an array. I know how to use the formatter for a single item but how can I format all the dates in my array? The format that I would like is (Month Day). Here's my code: NSMutableArray* dates = [[NSMutableArray alloc] init]; int

Remove particular objects from an array based on objects from another array

守給你的承諾、 提交于 2019-12-22 00:37:11
问题 Setup: Have a UITableView which shows US golf courses with name, street, state etc. UITableView's data source is a NSMutableArray of objects from my class GolfCourse called allGolfCourses . Now I like to remove all west coast golf courses from allGolfCourses and create a new array called eastCoastGolfCourses . I have another NSArray with string objects of all west coast states (Abbreviations) called westCoastStates but having a hard time connecting these two. How do I iterate through