Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取一个Objective-C方法以返回多个NSString_Objective C_String_Pointers - Fatal编程技术网

获取一个Objective-C方法以返回多个NSString

获取一个Objective-C方法以返回多个NSString,objective-c,string,pointers,Objective C,String,Pointers,我需要使用一个方法,根据一个值返回几个不同的字符串。 我的代码如下所示: 我不能让它工作!我得到的只是空字符串。我感觉对字符串的调用有点错误。请提供帮助。更新说明:我个人不将此方法作为第一件事来使用。如果可能的话,我会使用数组。不确定是否可以将此与ARC一起使用。一些Apple方法使用这种方法返回错误值,因此最好注意它 调用方法时,将复制传递给函数的那些参数(当然,它们不会获得复制消息,指针会复制到传递指针的最远位置),这些副本将在函数内部使用(或者更严格地说,是由参数值启动的)。因此,如果您

我需要使用一个方法,根据一个值返回几个不同的字符串。 我的代码如下所示:


我不能让它工作!我得到的只是空字符串。我感觉对字符串的调用有点错误。请提供帮助。

更新说明:我个人不将此方法作为第一件事来使用。如果可能的话,我会使用数组。不确定是否可以将此与ARC一起使用。一些Apple方法使用这种方法返回错误值,因此最好注意它

调用方法时,将复制传递给函数的那些参数(当然,它们不会获得复制消息,指针会复制到传递指针的最远位置),这些副本将在函数内部使用(或者更严格地说,是由参数值启动的)。因此,如果您更改副本(即尝试替换指针上的对象,可以修改可变对象),这将不会反映在值上。您可以使用指针(即,将指针传递给指针)来解决此问题

因此:

您可以这样做:

- (void) method: (NSString**)str {
    *str = @"Some string";
}
然后:

// string = "old vlalue"
NSString* string = @"old value";

// string = "Some string"
[self method:&string];

你不能让它工作,因为当你这样做的时候

string1 = @"The first string";
您只需覆盖本地参数并更新其引用,但不会修改被调用方之外的任何内容。因此,该值仅在函数范围内更改

您应该将方法的签名更改为

- (NSArray*) returnStringsAccordingToSet:(NSString *)string1: (NSString *)string2: (NSInteger)setNo {
因此它返回一个NSArray,而不是nothing,然后在函数内部

case1:
  return [NSArray arrayWithObjects:@"The first string",@"The second string",nil];
因此,返回数组并从调用方存储它,然后可以访问返回的值

NSArray *v = [returnStringAccordingTo: ..];
[v objectAtIndex:0]
从技术上讲,由于Objective是C的超集,我想可以通过引用传递指向
NSString
指针的指针(但在ObjC中不建议这样做):


您可以通过填充一个NSArray来实现这一点,但为了说明C是如何工作的,我将显示您所犯的错误。
string1和string2只是指针,因此您可以将可变或不可变字符串作为参数传递。但原始指针永远不会被修改,例如:

NSString* string1=@"Hello";  // Let's suppose string1 is on the 0x8000 address
                             // And points to the 0x9000 address
NSString* string2=@"How are you?";  // string2: 0x1000 , pointing to 0x2000
[self returnStringsAccordingToSet: string1: string1 string2: string2];
然后,当调用方法时,将为传递给该方法的每个指针创建一个副本:

- (void) returnStringsAccordingToSet:(NSString *)string1: (NSString *)string2: (NSInteger)setNo {
// string1 and string2 both point on the same address, but they are other pointers
// for example string1 is on 0x7000 and string2 on 0x7400, pointing to 0x9000
// and 0x2000
    switch (setNo){
        case 1:
            if (generalStringSettings){
                string1 = @"The first string";  // You just create another instance
                string2 = @"The second string"; // of the string, but the original
                // pointers aren't affected
            } else {
                string1 = @"The first other string";
                string2 = @"The second other string";
            }
            break;
        case 2:
             ...
             break;
        case 3:
             ...
             break;
    }
}
解决办法:

  • 将NSMutableString作为参数传递,而不是分配它们,只需修改内容
  • 将指针传递给指针
  • 返回包含新字符串的NSArray

  • 通常,这可以通过返回包含字符串的NSArray或NSDictionary来完成。但是,您也可以传递间接引用,即引用指向数组的指针(使用
    &
    )而不是指针中的值。可以使用另一个间接级别(但使用返回对象的语言)更改已传递的参数,这真的不是一个好主意。@HotLicks谢谢你-我会尝试相应地修改nt代码…它就像一个符咒…当我开始使用NSArray:s时,代码看起来更整洁了!!!谢谢你的解释。一个值得在书中占有一席之地的答案。。。。但ARC不喜欢这种方法。我不应该使用“&”。你的描述不正确。未制作字符串的副本。将创建指针的副本,而不是对象的副本。如果您将一个可变对象作为参数传入,那么对方法中可变对象所做的更改也会反映在调用该方法的对象中。@rmaddy我只是想简化一下,请看(从值部分开始),我真的觉得很容易理解我描述的方法,我将更新答案。@rmaddy,请注意,我没有提到对象被复制,我写了参数被复制,参数是指针。只是不够清楚。@MANIAK_dobrii您的原始答案(作为代码中的注释)清楚地表明该对象是复制的。无论如何,你的更新更好。
    NSString *string = nil;
    [self callMethod:&string];
    
    -(void)callMethod:(NSString**)refToString {
      *refToString = @"foobar";
    
    NSString* string1=@"Hello";  // Let's suppose string1 is on the 0x8000 address
                                 // And points to the 0x9000 address
    NSString* string2=@"How are you?";  // string2: 0x1000 , pointing to 0x2000
    [self returnStringsAccordingToSet: string1: string1 string2: string2];
    
    - (void) returnStringsAccordingToSet:(NSString *)string1: (NSString *)string2: (NSInteger)setNo {
    // string1 and string2 both point on the same address, but they are other pointers
    // for example string1 is on 0x7000 and string2 on 0x7400, pointing to 0x9000
    // and 0x2000
        switch (setNo){
            case 1:
                if (generalStringSettings){
                    string1 = @"The first string";  // You just create another instance
                    string2 = @"The second string"; // of the string, but the original
                    // pointers aren't affected
                } else {
                    string1 = @"The first other string";
                    string2 = @"The second other string";
                }
                break;
            case 2:
                 ...
                 break;
            case 3:
                 ...
                 break;
        }
    }