变量不是 CFStringRef

发布于 2024-12-07 07:12:38 字数 804 浏览 2 评论 0原文

我有这样的:

partenaire_lat = 48.8160525; 
partenaire_lng = 2.3257800;

并获得这样的 NSString:

NSString *endPoint =[NSString stringWithFormat:@"%@,%@", partenaire_lat, partenaire_lng];

在某些上下文中使用这个 NSString 后,我得到这个愚蠢的错误:

Variable is not a CFString.

但如果我像这样创建 NSString:

endPoint = @"48.8160525,2.3257800" 它然后就完美了!

对于这个错误 Variable is not a CFString 我尝试了以下操作:

NSString *endPoint1 =[NSString stringWithFormat:@"%@,%@", partenaire_lat, partenaire_lng];
CFStringRef endPoint =(CFStringRef)endPoint1;

并尝试使用 endPoint 但这种方式都不起作用。有人有什么神奇的想法吗?谢谢

EDIT:partenaire_lat and partenaire_lng are both NSString!!

I have this:

partenaire_lat = 48.8160525; 
partenaire_lng = 2.3257800;

And obtain a NSString like this:

NSString *endPoint =[NSString stringWithFormat:@"%@,%@", partenaire_lat, partenaire_lng];

and after using this NSString in some context I get this stupid error:

Variable is not a CFString.

But if I create the NSString like this:

endPoint = @"48.8160525,2.3257800" it then works perfect!

For this error Variable is not a CFString I tried the following:

NSString *endPoint1 =[NSString stringWithFormat:@"%@,%@", partenaire_lat, partenaire_lng];
CFStringRef endPoint =(CFStringRef)endPoint1;

and tried to use endPoint but not working neither this way.Anyone any miraculous idea?Thx

EDIT:partenaire_lat and partenaire_lng are both NSString!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甩你一脸翔 2024-12-14 07:12:38

partenaire_lat = 48.8160525; 
partenaire_lng = 2.3257800;

一直说这两个变量是 NSString ,但您没有为它们分配 NSString 。您需要将 NSString 对象分配给 NSString 变量 - 它们不会自动为您创建。

因此,告诉您使用格式化字符串的答案是正确的。你真的应该这样做:

partenaire_lat = [[NSString stringWithFormat:@"%f", 48.8160525] retain]; 
partenaire_lng = [[NSString stringWithFormat:@"%f", 2.3257800] retain];

You have

partenaire_lat = 48.8160525; 
partenaire_lng = 2.3257800;

You keep saying that the two variables are NSStrings but you aren't assigning NSStrings to them. You need to assign NSString objects to NSString variables - they aren't created for you automatically.

So the answers which are telling you to use formatted strings are correct. You really should be doing it like this:

partenaire_lat = [[NSString stringWithFormat:@"%f", 48.8160525] retain]; 
partenaire_lng = [[NSString stringWithFormat:@"%f", 2.3257800] retain];
白龙吟 2024-12-14 07:12:38

纬度和经度是什么?我假设 float 或 double ..所以您应该使用 [NSString stringWithFormat:@"%f,%f", lat, lng]; (或者您希望格式化浮点数)

what are lat and lng? i'm assuming float or double..so you should use [NSString stringWithFormat:@"%f,%f", lat, lng]; (or however you want the floats to be formatted)

反目相谮 2024-12-14 07:12:38

您的代码有几个潜在的问题:

  1. %@ 格式说明符需要对象参数,而看起来您传递的是普通浮点数(我在这里可能是错的,因为没有足够的上下文来确定)。如果确实如此,请将格式更改为 %f 以解决您的问题:

    NSString *endPoint1 =[NSString stringWithFormat:@"%f,%f",partenaire_lat,partenaire_lng];
    
  2. 您的 endPoint1 字符串是自动释放的,如果您不保留它,它可能会在当前范围之外变得无效。因此,如果您尝试在其他方法中使用您的变量,您可能应该保留它。

You code has several potential problems:

  1. %@ format specifier expects object parameter, while it looks like you pass plain float (I may be wrong here as there's not enough context to be sure). Change format to %f to fix your problem if that's really the case:

    NSString *endPoint1 =[NSString stringWithFormat:@"%f,%f", partenaire_lat, partenaire_lng];
    
  2. Your endPoint1 string is autoreleased and may become invalid outside of current scope if you don't retain it. So if you try to use your variable in another method you probably should retain it.

酒解孤独 2024-12-14 07:12:38

您需要做的

NSString *latStr=[[NSNumber numberWithFloat:partenaire_lat] stringValue];
NSString *lngStr=[[NSNumber numberWithFloat:partenaire_lng] stringValue];

就是用这两个字符串做任何您想做的事情:)

All you need to do

NSString *latStr=[[NSNumber numberWithFloat:partenaire_lat] stringValue];
NSString *lngStr=[[NSNumber numberWithFloat:partenaire_lng] stringValue];

and do whatever you want to do with these two string :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文