nsarray

Finding most repeated object in array

元气小坏坏 提交于 2019-12-05 08:54:24
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 alloc] initWithObjects @"james", @"megan", @"lauren", @"mike" @james", nil]; //james would be the most

How do I extend an NSArray?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 07:20:18
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* strings = [[NSArray alloc] initWithObjects: @"One.", nil]; return strings; } @end NSArray is

Copying NSDictionary to NSArray

江枫思渺然 提交于 2019-12-05 06:27:09
问题 I have a variable declared as NSDictionary . How can I copy it to an extern const NSArray variable? For example: NSMutableDictionary *selectedItem = [self.dataArray objectAtIndex:indexPath.row]; [selectedItem setObject:[NSNumber numberWithBool:targetCustomCell.checked] forKey:@"checked"]; 回答1: allKeys returns an NSArray containing all the keys. allValues returns an NSArray containing all the values. NSArray *keys = [myDict allKeys]; NSArray *values = [myDict allValues]; 来源: https:/

Accessing NSArray's items with subscript

早过忘川 提交于 2019-12-05 05:03:46
Is it possible to access NSArray's objects using [idx]? I have a standard library that uses [] style indexing and I don't want to rewrite the whole library to suit ObjC's objectAtIndex: method. As in, NSArray *obj = [NSArray ...]; id item = obj[0]; Nick Lockwood The accepted answer (whilst true at the time) is now out of date. As of Xcode 4.5, you can now set and get NSArray elements using: id object = array[5]; // equivalent to [array objectAtIndex:5]; mutableArray[5] = object; // equivalent to [mutableArray replaceObjectAtIndex:5 withObject:object]; You can also do the same for

NSString stringWithContentsOfURL is deprecated. What should I do?

允我心安 提交于 2019-12-05 04:06:07
I've written some code that uses stringWithContentsOfURL , but it turns out that it is now deprecated. Can you help me replace it? Here's my code: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]; NSArray *listItems = [locationString componentsSeparatedByString:@","]; Additionally, can someone have some sample code to show how it works? What encoding was the original code done in (in my 2 lines above)? I want to maintain compatibility so what should I choose for the encoding? What does error mean and what is it used for? Use stringWithContentsOfURL

NSPredicate to filter between two dates of NSString type

一个人想着一个人 提交于 2019-12-05 03:43:51
问题 I've some data (as NSDictionary in an NSMutableArray ), a sample data is like, Consider each row as NSDictionary and entire table is an NSMutableArray contains events. I want to result between two dates, so I'm using NSPredicate to filter my data. Here's my code snippet for this, NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Date >= %@ AND Date <= %@", startDate,endDate]; NSArray *filter = [arrayEvents filteredArrayUsingPredicate:predicate]; Test filtration : Case 1: NSString

How can I convert the NSString to a array?

↘锁芯ラ 提交于 2019-12-05 02:24:20
问题 For example, I read a data like this a\tbcd\tttte\tjjjd\tnjnjnjd\tss\tee and I want to make a array like this: { @"a", @"bcd", @"ttte", @"jjjd", @"njnjnjd", @"ss", @"ee" } How can I do so? Thank you. 回答1: You can use -componentsSeparatedByString: , as in NSArray *ary = [mystring componentsSeparatedByString:@"\t"]; 回答2: - (NSArray *)componentsSeparatedByString:(NSString *)separator componentsSeparatedByString: Returns an array containing substrings from the receiver that have been divided by a

Detect next UILocalNotification that will fire

夙愿已清 提交于 2019-12-05 01:52:53
问题 Is there a way to find the NSDate for the next local notification that will fire? For example, I've set up three local notifications: Notification #1: Set to fire yesterday at 3:00 PM with repeat interval of daily. Notification #2: Set to fire today at 5:00 PM with repeat interval of daily. Notification #3: Set to fire tomorrow at 6:00 PM with repeat interval of daily. Given that is is currently 4:00 PM, the next local notification that will fire is notification #2. How can I retrieve this

Create NSIndexSet from integer array in Swift

半世苍凉 提交于 2019-12-04 22:11:41
I converted an NSIndexSet to an [Int] array using the answer at https://stackoverflow.com/a/28964059/6481734 I need to do essentially the opposite, turning the same kind of array back into an NSIndexSet. Alexander Swift 3 IndexSet can be created directly from an array literal using init(arrayLiteral:) , like so: let indices: IndexSet = [1, 2, 3] Original answer (Swift 2.2) Similar to pbasdf's answer , but uses forEach(_:) let array = [1,2,3,4,5,7,8,10] let indexSet = NSMutableIndexSet() array.forEach(indexSet.add) //Swift 3 //Swift 2.2: array.forEach{indexSet.addIndex($0)} print(indexSet) This

Parse XML item “Category” into an NSArray with NSDictionary

倖福魔咒の 提交于 2019-12-04 22:05:26
Question: The generated XML file from an .XSD template produces the output pasted below. Since category 's keys are image , my standard way of parsing the image items wont work on category . I would like to place the category name="Category #" into an array, How can I make an array from the category fields. What I need: What I want is an array of dictionaries. Each position contains a dictionary representing one category, and each dictionary contains images for that category. Array: @"Category 1",@"Category 2",@"Category 3"; For each Array, a Dictionary with: <image> and everything in between