nsstring

Objective-C constants: NSString comparison using ==?

▼魔方 西西 提交于 2019-12-01 06:44:38
The discussions I found about setting NSString constants made me code it the following way: .h file: extern NSString * const kSectionHeaders; .m file: NSString * const kSectionHeaders = @"header"; As the program runs, it has to test words from a text file against a series of NSString constants. I read memory comparison should work when setting function like stated above: if (property == kSectionHeaders) {...} Doesn't work tough :( The following works, but it was described as a bad solution (slower, what else?): if ([property isEqualToString:kSectionHeaders]){...} I feel I've done something

NSSortDescriptor with arbitrary sorting

ぐ巨炮叔叔 提交于 2019-12-01 06:36:43
I can't wrap my head around how to do arbitrary sorting with a NSSortDescriptor. I want to do something like this: NSArray *sortAlgorithm = [NSArray arrayWithObjects:@"@", @"#", @"!", @"&", @"r", @"a", nil]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES comparator: ^(id obj1, id obj2) { NSComparisonResult comparisonResult; //Some code that uses sortAlgorithm. return comparisonResult; } ]; This would sort the objects by the key name so that any key that starts with @ , e.g. @home , would come before any key that starts with r , e.g. radical , and

How to convert NSString to UIColor

时光总嘲笑我的痴心妄想 提交于 2019-12-01 06:34:54
问题 I have a pickerView which has a component of colors. I have a textField (textOther), now I have NSMutableArray of all the available colors which are explicitly defined. How do I pass all those colors(which are strings!) to these textField? textField setTextColor will set only a single color, but to pass string to a color how do I do it? Thanks! 回答1: I suppose the cleanest way to do this would be by making an NSDictionary with predefined UIColor instances, and map them as key value pairs; keys

using UITextPosition, get previous character from textInRange

烈酒焚心 提交于 2019-12-01 06:28:24
问题 I've been searching high and low and can't figure out how to do this. Lets say I have a string like this: NSString *string = @"&foo !foo"; What I'm trying to do is differentiate between the two foos (sounds funny haha) inside a text view. When I click on one, I want to know which (& or !) comes before it. Here's how I'm getting the word: UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender; UITextView *TV = (UITextView*)recognizer.view; CGPoint location = [recognizer

How to compare A to Á on the iPhone

吃可爱长大的小学妹 提交于 2019-12-01 06:22:12
问题 I need to compare 2 strings, looking at the first letter only. Is there a method to compare A to Á, and recognize it as A, without the ´ ? 回答1: NSString has a diacritic-insensitive comparison mode which will do what you're after. // should return NSOrderedSame, i.e. identical [@"Apple" compare:@"Ápple" options:NSDiacriticInsensitiveSearch] If you want it to be case-insensitive as well: // ditto [@"APPLE" compare:@"Ápple" options:NSDiacriticInsensitiveSearch | NSCaseInsensitiveSearch] 来源:

52个有效方法(1) - 了解Objective-C语言的起源

强颜欢笑 提交于 2019-12-01 06:16:43
Objective-C语言使用的是"消息结构"而非"函数调用"。 "消息结构"和"函数调用"之间的区别 "消息结构"的语言: 运行时由运行环境决定所应执行的代码 "函数调用"的语言: 由编译器决定 //Messaging (Objective-C) 消息结构 Object *obj = [Object new]; [obj performWith:parameter1 and:parameter2]; //Function calling (C++)方法调用 Object *obj = new Object; obj->perform(parameter1,parameter2); 内存模型:Objective-C语言中的指针是用来指示对象的。 Objective-C为C语言添加了面向对象特性,是其"超集",C语言的所有功能在编写OC代码时依然适用! Objective-C语言中的指针是用来指示对象的。 对象所占内存总是分配在"堆空间"(heap space)中,不能在栈(stack)中分配对象。 NSString *someString = @"the string"; /**说明: 上面代码是声明一个名为someString的变量,类型是NSString *。看就是说,此变量为指向NSString的指针。 Objective-C对象所占内存分配在“堆空间”中,绝对不会分配在“栈

Currency Formatter for different Locale in iOS

丶灬走出姿态 提交于 2019-12-01 06:12:21
I want to convert this String: 1000000 to following format: 10,00,000 but, I am getting output like this : 1,000,000 which is wrong. I need 10,00,000 . My Code : NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; inputnumber = newString; NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [formatter setCurrencySymbol:@""]; [formatter setMaximumFractionDigits:2]; [formatter setMinimumFractionDigits:2]; [formatter setUsesGroupingSeparator:YES]; [formatter

Question about character of NSString invalid in URL on iPhone

流过昼夜 提交于 2019-12-01 06:03:01
Some of my http request has 'space' in URL, but it is not recognized by iPhone. I have to change 'space' to '%20' whenever there is a 'space' in the url. Now I have to send a message typed by the user to the server and it can have as many 'spaces' as the user like. It looks like I have to replace them all.(stringByReplacingOccurrencesOfString) Convert 'space' to '%20' is the one I occasionally find. My question is which of the other characters in NSString cannot be straightly used by url on iPhone? I think this is a better approach: NSString* escapedUrl = [originalUrl

iOS NSLog error with Unicode characters

倖福魔咒の 提交于 2019-12-01 05:58:10
Can anyone tell me the cause of the discrepancy for the following results ? completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"\n placemarks%@", placemarks); Resulting: placemarks( "\U039b\U03b5\U03c9\U03c6\U03cc\U03c1\U03bf\U03c2 \U039a\U03cd\U03c0\U03c1\U03bf\U03c5 163, 16451 \U0391\U03c1\U03b3\U03c5\U03c1\U03bf\U03cd\U03c0\U03bf\U03bb\U03b7\U03c2, \U0395\U03bb\U03bb\U03ac\U03c2 @ <+37.90085408,+23.75654648> +/- 100.00m, region (identifier <+37.90085408,+23.75654648> radius 141.62) <+37.90085408,+23.75654648> radius 141.62m" ) and for(id object in placemarks ) {NSLog(@"%@ \n"

NSString representation of fractions using unicode

耗尽温柔 提交于 2019-12-01 05:48:35
I am trying to "nicely" display fractions in my iPhone application. Previously I have been using a tedious switch statement leading to hardcoded unicode characters for the vulgar fractions, but I have learnt about the unicode fraction slash character which, if I am understanding it correctly, should mean that I can create a string as follows: [NSString stringWithFormat:@"%i\u2044%i",numerator,denominator]; And the "renderer" will automatically print it with a smaller, superscriped numerator and subscripted denominator. However, the above code just gives me the standard 1/2 appearance. I am