nsstring

How to search a subString from a NSString with case-insensitive

瘦欲@ 提交于 2019-11-30 05:38:36
I knew a instance method of NSString. - (NSRange)rangeOfString:(NSString *)aString My source string looks like this: "abcdEFGhi" When I use [srcStr.rangeOfString:@"efg"]; , there is no substring was found. How can I solve this problem? There is one more version of rangeOfString method you could use that. - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask So use as below [srcStr rangeOfString:@"efg" options:NSCaseInsensitiveSearch]; I think you're looking for: [srcStr rangeOfString:@"efg" options:NSCaseInsensitiveSearch]; 来源: https://stackoverflow.com/questions

Calculate number of differences between two NSStrings

对着背影说爱祢 提交于 2019-11-30 05:26:47
How can I calculate the number of differences between two NSStrings. Example: NSString 1 = "this is a string" NSString 2 = "Tihs isa string" should return: 4 (one for the capital "T", one for the "i", the "h" and for the missing space) Bart Kiers What you're looking for is the Levenshtein Distance . An implementation in Objective-C: ------------------------------------------------------------------------ // // NSString-Levenshtein.h // // Created by Rick Bourner on Sat Aug 09 2003. // rick@bourner.com @interface NSString(Levenshtein) // calculate the smallest distance between all words in

how to Check NSString is null or not [duplicate]

笑着哭i 提交于 2019-11-30 04:47:15
This question already has an answer here: How to detect if NSString is null? 5 answers I want to check weather a NSString is null or not. Im assigning from an JSON array. After assigning that string value is <null> . Now I want to check this string is null or not. So I put like this if (myStringAuthID==nil) but this if statement always false. How I can check a string for null. Please help me Thanks Mirko Catalano Like that: [myString isEqual: [NSNull null]]; There are three possible interpretations of "null" NSString: someStringPtr == nil (id)someStringPtr == [NSNull null] someStringPtr.length

Difference between these two NSString methods

旧巷老猫 提交于 2019-11-30 04:45:25
问题 So I just got asked this at an interview today and after some googling am still unable to figure out the answer (in fact I couldn't even find any code at all which used the [NSString string] method). What is the difference between NSString *someString = [NSString string]; NSString *someString = [[NSString alloc] init]; Now my initial thoughts were that [NSString string] would return an object which would be autoreleased whereas using alloc and init would return an object which has been

Cleaning malformed UTF-8 data

末鹿安然 提交于 2019-11-30 03:59:43
问题 Background With Swift, I'm trying to fetch HTML via URLSession rather than by loading it into a WKWebView first as I only need the HTML and none of the subresources. I'm running into a problem with certain pages that work when loaded into WKWebView but when loaded via URLSession (or even a simple NSString(contentsOf: url, encoding String.Encoding.utf8.rawValue) ) the UTF-8 conversion fails. How to reproduce This fails (prints "nil"): print(try? NSString(contentsOf: URL(string: "http://www

URL-encoding and HTML-encoding NSStrings

强颜欢笑 提交于 2019-11-30 03:50:06
问题 Is their a method to encode/decode HTML and URL (in Xcode, using Objective-C)? [NSString stringWithContentsOfFile:<#(NSString *)path#> encoding:<#(NSStringEncoding)enc#> error:<#(NSError **)error#>] This doesn't seem to work how i expected. I thought it will convert special characters like "<" to equivalent HTML entities i.e. "<" in this case. Here's a reference to the w3school link related to this topic (general): HTML URL Encoding Reference HTML Entities Reference Thanking in anticipation.

How to split newline from NSString in ObjectiveC

妖精的绣舞 提交于 2019-11-30 03:34:49
问题 For my new project. i have loaded my csv file content as a NSString. First, i need to split this by newline, then split each line by comma. How can i loop all this? Could you please help me? CSV Content "^GSPC",1403.36,"4/27/2012","4:32pm",+3.38,1400.19,1406.64,1397.31,574422720 "^IXIC",3069.20,"4/27/2012","5:30pm",+18.59,3060.34,3076.44,3043.30,0 ViewController.m NSString* pathToFile = [[NSBundle mainBundle] pathForResource: @"quotes" ofType: @"csv"]; NSString *fileString = [NSString

原子性atomic/nonatomic

不想你离开。 提交于 2019-11-30 03:27:49
原子性:并发编程中确保其操作具备整体性,系统其它部分无法观察到中间步骤,只能看到操作前后的结果。 决定编译器生成的getter和setter是否原子 (natomic) 操作。 i 因此, atomic 效率较低,因为要保证操作完整,线程相对安全; nonatomic 不加同步,多线程并发访问效率高,但线程不安全。 由于锁定机制开销较大,一般 iOS 开发中会使用 nonatomic。 atomic 也不是绝对的线程安全。当多个线程同时调用 setter 和 getter 时,就会导致获取的值不一样。 兼顾线程安全和效率的方案 要想线程绝对安全,就要使用 @synchronized 同步锁。 // 同步锁 -(NSString *)name{ @synchronized(self) { return _name; } } -(void)setName:(NSString *)name{ @synchronized(self) { _name = name; } } 但是由于同步锁有等待操作,会降低代码效率。 为了兼顾线程安全和提升效率,可采用 GCD 并发队列进行优化。getter 使用同步派发,setter 使用异步栅栏。 // 并发队列 _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

Get from AnyObject(NSString) to String

喜你入骨 提交于 2019-11-30 03:23:05
I am reading a plist key (NSArray with n NSDictionaries): let regionsToMonitor = NSBundle.mainBundle().infoDictionary["Regions"] as Array<Dictionary<String,AnyObject>> now I iterate over it: for regionToMonitor in regionsToMonitor { and now I want to to get uuidString of the regionToMonitor in ObjC: NSString *uuidString = regionToMonitor[@"uuidString"]; in swift I try: let uuidString = regionToMonitor["uuid"]!.stringValue; the above does compile but the string is always nil in swift. regionToMonitor["uuid"] when used without !.stringValue works fine in println how do I get a valid Swift.String

Replace occurrences of NSString - iPhone

最后都变了- 提交于 2019-11-30 03:13:31
I have a long NSString in which I m trying to replace special characters. Part of my string looks like this: "veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)" I would like to replace all the \u0153 with "oe". I've tried: [response stringByReplacingOccurrencesOfString:@"\u0153" withString:@"oe"]; but it doesn't work.... I don't understand why! The backslash is an escape character , so if you want to specify the actual backslash character in a string literal, you need to use two backslashes. NSString *new = [old