在我的iOS 5应用程序中,我有一个NSString包含JSON字符串的应用程序.我想将JSON字符串表示反序列化为本机NSDictionary对象.
"{\"password\" : \"1234\", \"user\" : \"andreas\"}"
Run Code Online (Sandbox Code Playgroud)
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:@"{\"2\":\"3\"}"
options:NSJSONReadingMutableContainers
error:&e];
Run Code Online (Sandbox Code Playgroud)
-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c'
Run Code Online (Sandbox Code Playgroud) 我试图检查我将用作URL的字符串是否以http开头.我现在试图检查的方式似乎不起作用.这是我的代码:
NSMutableString *temp = [[NSMutableString alloc] initWithString:@"http://"];
if ([businessWebsite rangeOfString:@"http"].location == NSNotFound){
NSString *temp2 = [[NSString alloc] init];
temp2 = businessWebsite;
[temp appendString:temp2];
businessWebsite = temp2;
NSLog(@"Updated BusinessWebsite is: %@", businessWebsite);
}
[web setBusinessWebsiteUrl:businessWebsite];
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在实施推送通知.我想将我的APNS令牌保存为字符串.
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken
{
NSString *tokenString = [NSString stringWithUTF8String:[newDeviceToken bytes]]; //[[NSString alloc]initWithData:newDeviceToken encoding:NSUTF8StringEncoding];
NSLog(@"%@", tokenString);
NSLog(@"%@", newDeviceToken);
}
Run Code Online (Sandbox Code Playgroud)
第一行代码打印为null.第二个打印令牌.如何将newDeviceToken作为NSString?
在iOS 7中,方法:
- (CGSize)sizeWithFont:(UIFont *)font
constrainedToSize:(CGSize)size
lineBreakMode:(NSLineBreakMode)lineBreakMode
Run Code Online (Sandbox Code Playgroud)
和方法:
- (CGSize)sizeWithFont:(UIFont *)font
Run Code Online (Sandbox Code Playgroud)
不推荐使用.我怎样才能更换
CGSize size = [string sizeWithFont:font
constrainedToSize:constrainSize
lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)
和:
CGSize size = [string sizeWithFont:font];
Run Code Online (Sandbox Code Playgroud) 我有一个这样的字符串:@"10/04/2011"我想在另一个字符串中只保存"10".我怎样才能做到这一点?
我想知道如何大写在一个对象中找到的字符串NSMutableArray.
一个NSArray包含字符串'April'索引2.
我想这是改变'APRIL'.
这样简单吗?
viewNoteDateMonth.text = [[displayDate objectAtIndex:2] capitalized];
Run Code Online (Sandbox Code Playgroud) 在Objective-C中处理大型文本文件的适当方法是什么?假设我需要分别读取每一行,并希望将每一行视为NSString.这样做最有效的方法是什么?
一种解决方案是使用NSString方法:
+ (id)stringWithContentsOfFile:(NSString *)path
encoding:(NSStringEncoding)enc
error:(NSError **)error
Run Code Online (Sandbox Code Playgroud)
然后使用换行符分隔符拆分行,然后遍历数组中的元素.但是,这似乎效率很低.有没有简单的方法将文件视为一个流,枚举每一行,而不是一次只读取它?有点像Java的java.io.BufferedReader.
我NSString喜欢这样的:
http://www.
Run Code Online (Sandbox Code Playgroud)
但我想将其转换为:
http%3A%2F%2Fwww.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
所以我有一个NSArray带有NSNumbers和NSStrings 的"myArray" .我需要他们在另一个,UIView所以我这样:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *details = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@"subject"];
Run Code Online (Sandbox Code Playgroud)
subjectText工作.但是我怎样才能从中获得NSNumber它?(其实我需要他们的细绳...)我想转换NSString出来的NSNumber是这样的:
NSString *blah = [NSNumber intValue].但我不知道如何在上面的代码中设置它...
我一直试图摆脱一个白色的空间NSString,但我尝试过的方法都没有.
我有"this is a test",我想得到"thisisatest".
我用过whitespaceCharacterSet,应该消除白色空间.
NSString *search = [searchbar.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
Run Code Online (Sandbox Code Playgroud)
但是我一直用空格来获得相同的字符串.有任何想法吗?