我正在尝试将CLLocation纬度/经度转换为字符串.我可以使用以下代码成功完成此操作:
// extract latitude from CLLocation object and cast to string
NSString *latitude = [[NSString alloc] initWithFormat:@"%g°", location.coordinate.latitude];
Run Code Online (Sandbox Code Playgroud)
这给了我一个像34.10111º的值.我希望这个数字是一个没有º度符号的纯字符串.
我应该用不同的格式初始化字符串吗?
我尝试使用格式@"%d"启动,字符串完全出现在另一个数字上.
我在我的应用程序中遇到了一些NSString的困难.基本上,我有一个名为o1string的NSString,它包含值"602".我想在UIAlertView中输出这个以及其他一些文本.
votedmessage = [ NSString stringWithFormat:@"The current standings are as follows:\n\n%@: %@ votes", b1title, o1string ];
UIAlertView *votedAlert = [[UIAlertView alloc] initWithTitle:@"Thank you for voting" message:votedmessage delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
Run Code Online (Sandbox Code Playgroud)
我已经使用了NSLog并验证了NSString中的值肯定是602,而消息中使用的另一个变量(b1title)自己输出正常.当我将o1votes变量添加到警报消息时,我无法弄清楚为什么应用程序崩溃了,是否与在NSString中只保留一个数字的冲突有关?
这是o1string的设置方式.它肯定包含"602",从XML文件中获取.
o1string = [[options objectAtIndex:3] objectForKey: @"votes"];
o1string = [o1string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
o1string = [o1string stringByReplacingOccurrencesOfString:@" " withString:@""];
Run Code Online (Sandbox Code Playgroud) 我正在构建一个通过NSStream发送到服务器的数据包.我试图用'§'(ascii代码167)分隔两个数据.这是服务器的构建方式,所以我需要尽量保持在这些范围内......
unichar asciiChar = 167; //yields @"§"
[self setSepString:[NSString stringWithCharacters:&asciiChar length:1]];
sendData=[NSString stringWithFormat:@"USER User%@Pass", sepString];
NSLog(sendData);
const uint8_t *rawString=(const uint8_t *)[sendData UTF8String];
[oStream write:rawString maxLength:[sendData length]];
Run Code Online (Sandbox Code Playgroud)
所以最终结果看起来应该是这样的......当sendData首次构造时它会这样做:
USER User§Pass
Run Code Online (Sandbox Code Playgroud)
但是,当它在服务器端收到时,它看起来像这样:
//not a direct copy and paste. The 'mystery character' may not be exact
USER UserˤPas
Run Code Online (Sandbox Code Playgroud)
...分隔符字符串的长度变为两个,最后一个字母从命令中被裁剪.我相信这是UTF8转换的原因.
任何人都可以为我阐明这一点吗?
任何帮助将不胜感激!
我有这样的NSString
NSString *mystring = @"RahulVyas";
Run Code Online (Sandbox Code Playgroud)
现在我想添加这个'所以新的字符串将是'RahulVyas'
这该怎么做 ?
对于我的应用程序,我需要使用Carbon文件管理器API来获取文件夹的大小(NSEnumerator很慢,使用带有shell命令的NSTask甚至更糟).我已导入Carbon框架,我正在使用此方法来获取文件夹的大小:
http://www.cocoabuilder.com/archive/message/cocoa/2005/5/20/136503
它使用FSRef作为参数,我的路径字符串当前是NSString.我尝试使用它将NSString转换为FSRef:
FSRef f;
OSStatus os_status = FSPathMakeRef((const UInt8 *)[filePath fileSystemRepresentation], &f, NULL);
if (os_status != noErr) {
NSLog(@"fsref creation failed");
}
Run Code Online (Sandbox Code Playgroud)
然后我调用了文件夹大小方法:
[self fastFolderSizeAtFSRef:f];
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试构建时,我收到有关上述行的错误:
错误:'fastFolderSizeAtFSRef:'参数之一的不兼容类型
任何帮助,将不胜感激.谢谢
我有一个NSString,我想将其值写入NSMutableString.这是有效的:
NSString *temp = [NSString stringWithString:@"test"];
NSMutableString *mutable = temp;
Run Code Online (Sandbox Code Playgroud)
我问,因为虽然这似乎可行,但我认为这会将temp和mutable分配给相同的地址空间.将值传递给方法或从方法返回值时,我有很多这个问题.有时我看到其他人这样做或者使用stringWithString或initWithString创建可变字符串.谢谢
我有一个定义:
hashdefine kPingServerToSeeIfInternetIsOn "http://10.0.0.8"
Run Code Online (Sandbox Code Playgroud)
然后在代码I中使用它:
NSString *theURL = [NSString stringWithFormat:@"%@", kPingServerToSeeIfInternetIsOn];
Run Code Online (Sandbox Code Playgroud)
我得到一个例外.
为应用程序定义const并在NSStringinit中使用它的最佳方法是什么?
我想从一个文件或一个objective-c字符串(在代码中)运行一个shell脚本.我还希望将shell脚本的结果存储到变量中.我不希望将shell脚本拆分为参数(例如运行时的setLaunchPath).例如:运行此shell脚本"mount_webdav idisk.mac.com/mac_username/Volumes/mac_username"而不是"/ bin/mount_webdav"然后运行参数.反正有没有这样做?我现在正在使用NSTask,但是当我尝试将参数与它一起使用时,它给我带来了一些错误.这是提出的代码:
(一些.m文件)
NSString *doshellscript(NSString *cmd_launch_path, NSString *first_cmd_pt) {
NSTask *task = [[NSTask alloc] init]; // Make a new task
[task setLaunchPath: cmd_launch_path]; // Tell which command we are running
[task setArguments: [NSArray arrayWithObjects: first_cmd_pt, nil]];
[task setArguments: first_cmd_pt];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[task release]; //Release the task into the world, thus destroying it.
return string;
}
NSString …Run Code Online (Sandbox Code Playgroud) 我有一个NSString我正在使用,但我想按字符长度解析它.因此将它分解为NSArray,并使数组中的每个对象都是该字符串中的x个字符.所以基本上,将字符串分解为一定长度的子字符串
那么,我该怎么做?
例:
NSString*string = @"这是我的字符串"
NSArray对象:
"她"
"我"
"s m"
"是的"
"三"
"NG"
在一个方法中,我有以下代码:
NSString* tempString = (NSString*)arg;
NSArray* notificationDetails = [tempString componentsSeparatedByString:@"##"];
NSString* tempString1 = [notificationDetails objectAtIndex:2];
.
.
.
.
.
.
NSLog(tempString1);
Run Code Online (Sandbox Code Playgroud)
当我编译代码时,它的编译没有任何错误和警告.但是在运行时(在带有断点的调试模式下)它在NSLog语句中崩溃.我遇到的麻烦是在获取"[notificationDetails objectAtIndex:2];"时 这是什么解决方案.
nsstring ×10
iphone ×5
cocoa ×4
objective-c ×4
nsarray ×2
cllocation ×1
cocoa-touch ×1
ios ×1
macos-carbon ×1
macros ×1
nsstream ×1
parsing ×1
scripting ×1
shell ×1
utf-8 ×1
xcode ×1