如何使用临时变量在 Objective-C / Xcode 中定义返回 NSString 函数?

发布于 2024-10-24 17:19:44 字数 821 浏览 6 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(3

蓝眸 2024-10-31 17:19:44
static NSstring *xx;

它声明了一个静态分配的变量,就像在 C 中一样。

NSstring *xx;

在声明普通局部堆栈变量的方法内部,就像在 C 中一样。

您应该知道,两者之间的区别在于,第一个将保留其函数调用之间的值(如果从多个线程调用该函数,可能会导致问题)。

[[NSString alloc] initWithString:@"etc etc"]

这将创建一个新的 NSString 对象,其内容为 etc。这可能与程序中具有相同内容的任何其他 NSString 对象相同,也可能不同,但您不必关心。就内存管理而言,您拥有它,因此您有责任确保最终对其调用 releaseautorelease 以避免内存泄漏。

@"etc etc"
[NSString stringWithString:@"etc etc"]

这些基本上是相同的。两者都会给你一个 NSString 对象,其内容为 etc。这可能与程序中具有相同内容的任何其他 NSString 对象相同,也可能不同,但您不必关心。在内存管理方面,您不拥有它,因此您不能在对象上调用 releaseautorelease ,除非您首先通过调用 <代码>保留。另外,由于您不拥有它,因此您可以在方法中使用它,将其作为参数传递给其他方法,甚至将其用作方法的返回值,但不能将其存储在 ivar 或静态变量中无需通过调用 retain 或制作副本(使用 copy)获取所有权。

另请注意,""@"" 非常不同。第一个为您提供了一个与 C 中完全相同的 const char *,而第二个为您提供了一个 NSString 对象。如果您在代码需要 NSString 对象的地方使用 const char *,您的程序将崩溃。

static NSstring *xx;

That declares a statically allocated variable, much like it does in C.

NSstring *xx;

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).

[[NSString alloc] initWithString:@"etc etc"]

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 call release or autorelease on it to avoid leaking memory.

@"etc etc"
[NSString stringWithString:@"etc etc"]

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 call release or autorelease on the object unless you first took ownership by calling retain. 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 calling retain or making a copy (with copy).

Also, note that "" and @"" are very different. The first gives you a const char * exactly as it does in C, while the second gives you an NSString object. Your program will crash if you use a const char * where the code expects an NSString object.

夕嗳→ 2024-10-31 17:19:44

你可以这样做:

- (NSString *)functionName:(int)param {
    NSString *result = nil;

    switch (param) {
        case 1:
            result = [NSString stringWithString:@"x"];
            break;
        case 2:
            result = [NSString stringWithString:@"y"];
            break;
        case 3:
            result = [NSString stringWithString:@"z"];
            break;
        default:
            result = [NSString stringWithString:@"defaultv"];
            break;
    }

    return result;
}

You can do it this way:

- (NSString *)functionName:(int)param {
    NSString *result = nil;

    switch (param) {
        case 1:
            result = [NSString stringWithString:@"x"];
            break;
        case 2:
            result = [NSString stringWithString:@"y"];
            break;
        case 3:
            result = [NSString stringWithString:@"z"];
            break;
        default:
            result = [NSString stringWithString:@"defaultv"];
            break;
    }

    return result;
}
哀由 2024-10-31 17:19:44

发布真实代码,而不是伪代码,因为它可以更轻松地具体回答您的问题。

鉴于您表明您对 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.

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