nsstring

What's the best way to cut Swift string into 2-letter-strings?

元气小坏坏 提交于 2019-12-18 09:32:29
问题 I need to split a string into 2-letter pieces. Like “friend" -> "fr" "ie" "nd". (Okay, its a step for me to change HEX string to Uint8 Array) My code is for i=0; i<chars.count/2; i++ { let str = input[input.startIndex.advancedBy(i*2)..<input.startIndex.advancedBy(i*2+1)] bytes.append(UInt8(str,radix: 16)!) } But I don't know why I cannot use Range to do this split. And I have no idea what will happen when i*2+1 is bigger than string's length. So what's the best way to cut Swift string into 2

NSJSONSerialization and Emoji

心已入冬 提交于 2019-12-18 06:58:30
问题 I'm currently trying to POST some JSON containing emojis to a python API. I tried feeding the NSJSONSerialization directly with the string containing the emojis from my UITextField but the serializer crashed with no meaningful explanation. Afterwards I tried to do some format conversion and ended up with something like this: NSString *uniText = mytextField.text; NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding]; NSString *goodMsg = [[NSString alloc] initWithData

AFNetworking框架使用

最后都变了- 提交于 2019-12-18 06:23:46
本文是由 iOS Tutorial 小组成员 Scott Sherwood 撰写,他是一个基于位置 动态加载( Dynamically Loaded )的 软件公司(专业的混合定位)的共同创办人。 网络 — 你的程序离开了它就不能生存下去!苹果的Foundation framework中的NSURLConnection又非常难以理解, 不过这里有一个可以使用的替代品: AFNetworking . AFNetworking 非常受开发者欢迎 – 它赢得了我们读者的青睐:2012年最佳的iOS Library奖( 2012 Best iOS Library Award .) 所以现在我就写这篇文章来向你介绍如何在程序中有效的使用它。 AFNetworking 包括了所有你需要与在线资源交互的内容,从web services到文件下载。当你的程序在下载一个大文件期间,AFNetworking还能确保你的UI是可以响应的。 本文将介绍AFNetworking框架主要的组成部分。一路上,你将使用 World Weather Online 提供的咨询(Feeds)来创建一个天气(Weather)程序。刚开始使用的天气数据是静态的,不过在学完本文内容之后,程序将连接到实时的天气咨询。 今日预计:一个很酷的开发者学习所有关于AFNetworking知识,并在他的程序中使用AFNetworking

Check if key exists in NSDictionary is null or not

不羁岁月 提交于 2019-12-18 05:40:13
问题 I did search on how to check if NSDictionary key exists or not and came up with the solution. But still it throws me an error saying adding null value to the key. I am not sure if my code is correct or not. If anyone has any idea about this can help me. NSDictionary *result; id myImageURL = [result objectForKey:@"url"]; if ((NSNull *)myImageURL == [NSNull null]) myImageURL = @""; id myImage = [result objectForKey:@"image"]; if ((NSNull *)myImage == [NSNull null]) myImage = @""; Check if null

Localizable.strings causing plist parsing error

随声附和 提交于 2019-12-18 05:39:29
问题 I have just localized the file Localizable.strings in my Xcode project in order to localise my application to a few different languages. However, having edited each of the files in the standard "key = value" format, I receive the following parsing error, which does not specify a file making it hard to track down what it is referring to. CFPropertyListCreateFromXMLData(): Old-style plist parser: missing semicolon in dictionary on line 10. Parsing will be abandoned. Break on

NSString to treat “regular english alphabets” and characters like emoji or japanese uniformly

Deadly 提交于 2019-12-18 05:20:12
问题 There is a textView in which I can enter Characters. characters can be a,b,c,d etc or a smiley face added using emoji keyboard. -(void)textFieldDidEndEditing:(UITextField *)textField{ NSLog(@"len:%lu",textField.length); NSLog(@"char:%c",[textField.text characterAtIndex:0]); } Currently , The above function gives following outputs if textField.text = @"qq" len:2 char:q if textField.text = @"😄q" len:3 char:= What I need is if textField.text = @"qq" len:2 char:q if textField.text = @"😄q" len:2

Remove double quotes from NSString

不想你离开。 提交于 2019-12-18 05:11:13
问题 How do I remove double quotes from an NSString. Example: //theMutableString: "Hello World" [theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}] It doesn't seem to work, maybe since I had to escape the " in the replaceOccurrencesOfString? 回答1: Use the NSMakeRange function instead of your cast. This'll work: [mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range

Remove double quotes from NSString

Deadly 提交于 2019-12-18 05:11:11
问题 How do I remove double quotes from an NSString. Example: //theMutableString: "Hello World" [theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}] It doesn't seem to work, maybe since I had to escape the " in the replaceOccurrencesOfString? 回答1: Use the NSMakeRange function instead of your cast. This'll work: [mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range

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

狂风中的少年 提交于 2019-12-18 04:05:25
问题 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

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

╄→尐↘猪︶ㄣ 提交于 2019-12-18 04:03:53
问题 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