如何使用临时变量在 Objective-C / Xcode 中定义返回 NSString 函数?
我想在 Objective-C 中定义以下函数。我提供了伪代码来帮助说明我正在尝试做什么。
伪代码:
function Foo(param) {
string temp;
if(param == 1) then
temp = "x";
else if(param == 2) then
temp = "y";
else if(param == 3) then
temp = "z";
else
temp = "default";
end if
return temp;
}
出于某种原因,如果我这样做...我分配给它的变量会导致“BAD Access”错误。
声明之间有什么区别
static NSstring *xx;
我不知道:或非静态:
NSString *xx;
,以及我如何或为什么要使用其中一个而不是另一个。
我也不完全理解 NSString 的初始化器以及它们有何不同。例如:
[[NSString alloc] initWithString:@"etc etc" ];
或简单的作业:
var = @""
甚至:
var = [NSString stringWithString:@"etc etc"];
你能帮我一下吗?
到目前为止,使用从上面列出的函数返回的 NSString 值总是会导致错误。
I would like to define the following function in Objective-C. I have provided pseudo-code to help illustrate what I'm trying to do.
PSEUDOCODE:
function Foo(param) {
string temp;
if(param == 1) then
temp = "x";
else if(param == 2) then
temp = "y";
else if(param == 3) then
temp = "z";
else
temp = "default";
end if
return temp;
}
For some reason if I do this... the variable who I assign it to results in a "BAD Access" error.
I don't know what the difference between:
static NSstring *xx;
or the non-static:
NSString *xx;
declarations are, and how or why I would want to use one over the other.
I also do not fully understand the initializers of NSString, and how they differ. For example:
[[NSString alloc] initWithString:@"etc etc" ];
or the simple assignment:
var = @""
or even:
var = [NSString stringWithString:@"etc etc"];
Can you give me a hand please?
So far, using the NSString value returned from functions like those listed above, always causes an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它声明了一个静态分配的变量,就像在 C 中一样。
在声明普通局部堆栈变量的方法内部,就像在 C 中一样。
您应该知道,两者之间的区别在于,第一个将保留其函数调用之间的值(如果从多个线程调用该函数,可能会导致问题)。
这将创建一个新的 NSString 对象,其内容为
etc
。这可能与程序中具有相同内容的任何其他 NSString 对象相同,也可能不同,但您不必关心。就内存管理而言,您拥有它,因此您有责任确保最终对其调用release
或autorelease
以避免内存泄漏。这些基本上是相同的。两者都会给你一个 NSString 对象,其内容为
etc
。这可能与程序中具有相同内容的任何其他 NSString 对象相同,也可能不同,但您不必关心。在内存管理方面,您不拥有它,因此您不能在对象上调用release
或autorelease
,除非您首先通过调用 <代码>保留。另外,由于您不拥有它,因此您可以在方法中使用它,将其作为参数传递给其他方法,甚至将其用作方法的返回值,但不能将其存储在 ivar 或静态变量中无需通过调用retain
或制作副本(使用copy
)获取所有权。另请注意,
""
和@""
非常不同。第一个为您提供了一个与 C 中完全相同的const char *
,而第二个为您提供了一个 NSString 对象。如果您在代码需要 NSString 对象的地方使用 const char *,您的程序将崩溃。That declares a statically allocated variable, much like it does in C.
Inside a method that declares a normal local stack variable, just as it does in C.
As you should be aware, the difference between the two is that the first will keep its value between invocations of the function (and can cause trouble if the function is called from multiple threads).
That creates a new NSString object, with the contents
etc etc
. This may or may not be the same as any other NSString object in your program with the same contents, but you don't have to care. Memory management wise, you own it, so you are responsible for ensuring that you eventually callrelease
orautorelease
on it to avoid leaking memory.Those are basically the same. Both give you an NSString object with the contents
etc etc
. This may or may not be the same as any other NSString object in your program with the same contents, but you don't have to care. Memory management wise, you do not own it, so you must not callrelease
orautorelease
on the object unless you first took ownership by callingretain
. Also, since you do not own it, you can use it within your method, pass it as a parameter to other methods, and even use it as the return value from your method, but you may not store it in an ivar or static variable without taking ownership by callingretain
or making a copy (withcopy
).Also, note that
""
and@""
are very different. The first gives you aconst char *
exactly as it does in C, while the second gives you an NSString object. Your program will crash if you use aconst char *
where the code expects an NSString object.你可以这样做:
You can do it this way:
发布真实代码,而不是伪代码,因为它可以更轻松地具体回答您的问题。
鉴于您表明您对 Objective-C 相当陌生,我建议您从 语言指南,然后转到 内存管理指南。
Post real code, not pseudo code, as it makes it much easier to answer your question in concrete terms.
Given that you indicate that you are quite new to Objective-C, I would suggest starting with the language guide and then moving on to the memory management guide.