为什么我的 NSString 值会在定义类中丢失?这正常吗?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有发布足够的信息。 :)
这意味着什么?显示字符串。
functionTest
是否在同一个源文件中定义?它是从哪里调用的?由于您使用的是常量字符串(@“”),因此不需要保留/释放,因此这不是问题。
这些真的是您正在使用的字符串值吗?
You aren't posting enough information. :)
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?
因为您从未拥有它的所有权,所以您的字符串将由自动释放池自动释放。在您的
didFinishLaunchingWithOptions:
方法中,尝试使用:并且不要忘记在 dealloc 方法中释放它
Because you never take ownership of it, your string is getting autoreleased by the auto release pool. In your
didFinishLaunchingWithOptions:
method, try using:And don't forget to release it int the dealloc method