nsstring

Trying to Write NSString sha1 function, but it's returning null

一个人想着一个人 提交于 2019-11-29 04:52:14
I have the following Objective-C function: +(NSString *)stringToSha1:(NSString *)str{ NSMutableData *dataToHash = [[NSMutableData alloc] init]; [dataToHash appendData:[str dataUsingEncoding:NSUTF8StringEncoding]]; unsigned char hashBytes[CC_SHA1_DIGEST_LENGTH]; CC_SHA1([dataToHash bytes], [dataToHash length], hashBytes); NSData *encodedData = [NSData dataWithBytes:hashBytes length:CC_SHA1_DIGEST_LENGTH]; [dataToHash release]; NSString *encodedStr = [NSString stringWithUTF8String:[encodedData bytes]]; //NSString *encodedStr = [[NSString alloc] initWithBytes:[encodedData bytes] // length:

KVO

▼魔方 西西 提交于 2019-11-29 04:41:08
KVO的全称是Key-Value Observing,俗称“键值监听”,可以用于监听某个对象属性值的改变 原理 利用RuntimeAPI动态生成一个子类,并且让instance对象的isa指向这个全新的子类 当修改instance对象的属性时,会调用Foundation的_NSSetXXXValueAndNotify函数 _NSSetXXXValueAndNotify的内部调用如下 调用willChangeValueForKey: 调用原来的setter实现 调用didChangeValueForKey;didChangeValueForKey:内部会调用observer的observeValueForKeyPath:ofObject:change:context:方法 相关问题 iOS用什么方式实现对一个对象的KVO?(KVO的本质是什么?) 答:如上 如何手动触发KVO? 当 +(BOOL)automaticallyNotifiesObserversForKey:(NSString )key 返回是 YES,那么注册的这个 Key 就会替换对应的 Setter ,从而在改变的时候调用 -(void)willChangeValueForKey:(NSString )key 与 -(void)didChangeValueForKey:(NSString *)key 发送通知给观察者

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

喜夏-厌秋 提交于 2019-11-29 04:38:37
问题 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? 回答1: 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]; 回答2: I think you're

Objective-C: how to group a series of string constants?

六月ゝ 毕业季﹏ 提交于 2019-11-29 03:58:36
I defined a series of string constants like below, in macro way, #define EXT_RESULT_APPID @"appid" #define EXT_RESULT_ERROR_CODE @"errorcode" #define EXT_RESULT_PROGRESS @"progress" ... All these constants are supposed to be used in same context, so I'd like to constraint them in a same namespace, and I don't want to make them global , just like what this post said. In the other hand, I could put all numeric constants in an enum but it doesn't work for strings. Then how could I group these related string constants? Here's one approach: MONExtResult.h // add __unsafe_unretained if compiling for

Truncate string containing emoji or unicode characters at word or character boundaries

↘锁芯ラ 提交于 2019-11-29 03:57:05
How can I truncate a string at a given length without annihilating a unicode character that might be smack in the middle of my length? How can one determine the index of the beginning of a unicode character in a string so that I can avoid creating ugly strings. The square with half of an A visible is the location of another emoji character which has been truncated. -(NSMutableAttributedString*)constructStatusAttributedStringWithRange:(CFRange)range NSString *original = [_postDictionay objectForKey:@"message"]; NSMutableString *truncated = [NSMutableString string]; NSArray *components =

Downloading HTML string into an NSString or Swift String variable in iOS

给你一囗甜甜゛ 提交于 2019-11-29 03:55:02
Alternative titles How to download a web page data in Objective-C on iPhone? How to read entire content of an html resource file into an NSString * How to download a web page data in Swift? Saving HTML pages in an NSString How do you get web page data and put the data into a string? Something like: NSString *webPageBody = GetWebPageData("www.mysite.com/webpage.html"); in the webPageBody , I would like to see "html my web page html". Swift 3.0 guard let url = URL(string: "http://www.example.com") else { return } do { let html = try String(contentsOf: url) } catch { //handle `error` here } Swift

Calculate number of differences between two NSStrings

馋奶兔 提交于 2019-11-29 03:54:25
问题 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) 回答1: 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

NSString by removing the initial zeros?

自闭症网瘾萝莉.ら 提交于 2019-11-29 03:53:26
How can I remove leading zeros from an NSString ? e.g. I have: NSString *myString; with values such as @"0002060" , @"00236" and @"21456" . I want to remove any leading zeros if they occur: e.g. Convert the previous to @"2060" , @"236" and @"21456" . Thanks. adali For smaller numbers: NSString *str = @"000123"; NSString *clean = [NSString stringWithFormat:@"%d", [str intValue]]; For numbers exceeding int32 range: NSString *str = @"100004378121454"; NSString *clean = [NSString stringWithFormat:@"%d", [str longLongValue]]; This is actually a case that is perfectly suited for regular expressions:

iOS 公用方法

别说谁变了你拦得住时间么 提交于 2019-11-29 03:37:45
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat size = 0.0; NSError *error; NSDictionary *dic = [[ NSFileManager defaultManager] attributesOfFileSystemForPath: NSHomeDirectory () error:&error]; if (error) { #ifdef DEBUG NSLog ( @"error: %@" , error.localizedDescription); #endif } else { NSNumber *number = [dic objectForKey: NSFileSystemSize ]; size = [number floatValue]/1024/1024; } return size; } 2. 获取磁盘可用空间大小 [Objective-C] 查看源文件 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 //磁盘可用空间 + (CGFloat)diskOfFreeSizeMBytes{ CGFloat size = 0.0;