为什么我的 NSString 值会在定义类中丢失?这正常吗?

发布于 2024-10-23 17:48:11 字数 1061 浏览 6 评论 0原文

我在 NSString 类型的类中定义一些简单的变量 其中之一是保留我的 sqlite 数据库路径,等等...... 我正在 AppDelegate 中初始化函数 didFinishLaunchingWithOptions 当我试图查看它在这个函数之外的价值时......它在运行时失去了它的价值(不,我没有重写或任何东西......) 这是我的代码...

app.h 文件

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
.....................
    NSString *site_domain;

    NSString *databaseName;
    NSString *databasePath;
..........................
}

app.m 文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    databaseName = @"database.sqlite";  
    site_domain=@"http://localhost/webservice";   

         NSLog(@"%@",site_domain);
// inside this function I can use the value, ..if I call whatever function from 
//this function, that function can also use the value...
}

-(void) functionTest {
   NSLog(@"%@",site_domain);
//here the value is lost, it's like it can't reach it's pointer or something...
//puts on a wierd string
}

我做错了什么?

I'm defining some simple variables in a class of type NSString
one of them is keeping my sqlite database path for example, and so on...
I am initializing that in the AppDelegate , function didFinishLaunchingWithOptions
and when I'm trying to see it's value outside this function... it's losing it's value durring runtime, (no I'm not overwrting or anything...)
here's my code...

app.h file

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
.....................
    NSString *site_domain;

    NSString *databaseName;
    NSString *databasePath;
..........................
}

app.m file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    databaseName = @"database.sqlite";  
    site_domain=@"http://localhost/webservice";   

         NSLog(@"%@",site_domain);
// inside this function I can use the value, ..if I call whatever function from 
//this function, that function can also use the value...
}

-(void) functionTest {
   NSLog(@"%@",site_domain);
//here the value is lost, it's like it can't reach it's pointer or something...
//puts on a wierd string
}

What am i Doing wrong?

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

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

发布评论

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

评论(2

GRAY°灰色天空 2024-10-30 17:48:11

我做错了什么?

您没有发布足够的信息。 :)

//here the value is lost, it's like it can't reach it's pointer or something...
//puts on a wierd string

这意味着什么?显示字符串。 functionTest 是否在同一个源文件中定义?它是从哪里调用的?

由于您使用的是常量字符串(@“”),因此不需要保留/释放,因此这不是问题。

这些真的是您正在使用的字符串值吗?

What am i Doing wrong?

You aren't posting enough information. :)

//here the value is lost, it's like it can't reach it's pointer or something...
//puts on a wierd string

What does that mean? Show the string. Is functionTest defined in the same source file? Where is it called from?

Since you are using constant strings (@""), there is no need for retain/release, so that isn't the problem.

Are those really the string values you are using?

﹎☆浅夏丿初晴 2024-10-30 17:48:11

因为您从未拥有它的所有权,所以您的字符串将由自动释放池自动释放。在您的 didFinishLaunchingWithOptions: 方法中,尝试使用:

site_domain = [[NSString alloc] initWithString:@"http://..."];

并且不要忘记在 dealloc 方法中释放它

-(void)dealloc
{
    [site_domain release];
}

Because you never take ownership of it, your string is getting autoreleased by the auto release pool. In your didFinishLaunchingWithOptions: method, try using:

site_domain = [[NSString alloc] initWithString:@"http://..."];

And don't forget to release it int the dealloc method

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