Objective-C - 我的结构中是否可以使用NSArray?

时间:2017-03-08 16:59:56

标签: objective-c struct

用于我的OSX和iOS库。

我试图在array中保留struct。但遗憾的是NSArray *array_url;内的struct不允许Xcode。

但是当我删除__unsafe_unretained NSArray *array_url并将其放入主块然后确定(但这不是我的目标)时,是不是可以将数组放在struct中?

#import <Foundation/Foundation.h>

struct OSX {
  __unsafe_unretained NSString *input_url;
  __unsafe_unretained NSString *use_url;    
  __unsafe_unretained NSArray *array_url; //USE it later in other place

};

int main(int argc, const char * argv[]) {
  @autoreleasepool {    
    struct OSX struct_OSX;
    struct_OSX.input_url = @"test1:test2:test3:..:..:99:9900";

    NSArray *struct_OSX.array_url = [struct_OSX.input_url componentsSeparatedByString:@":"];
    NSLog(@"%@", struct_OSX.array_url);

    for(NSArray *subArray in struct_OSX.array_url) {
      NSLog(@"%@", subArray);    
    }

    NSLog(@"%@", [struct_OSX.array_url objectAtIndex:1] );               
    // let me validate the array: if ([struct_OSX.array_url length] == 0) { }

  }

  return 0;

}

1 个答案:

答案 0 :(得分:-2)

struct与NSArray

#import <Foundation/Foundation.h>

struct iOS {
  __unsafe_unretained NSString *input_url;
  __unsafe_unretained NSString *use_url;
  __unsafe_unretained NSString *server;
  __unsafe_unretained NSString *agent;
  __unsafe_unretained NSString *roomid;
  __unsafe_unretained NSArray *array_url;
  __unsafe_unretained NSArray *array_url_modified;
};

int main(int argc, const char * argv[]) {
  @autoreleasepool {
    struct iOS StructIOS;
    StructIOS.input_url = @"https://a/b/c";

    //NSArray *work_array = [StructIOS.input_url componentsSeparatedByString:@"https://"];
    StructIOS.array_url = [StructIOS.input_url componentsSeparatedByString:@"https://"];
    NSLog(@"%@", StructIOS.array_url);

    for(NSArray *subArray in StructIOS.array_url) {
      NSLog(@"%@", subArray);
    }

    NSLog(@"%@", [StructIOS.array_url objectAtIndex:1] );
    StructIOS.use_url =[StructIOS.array_url objectAtIndex:1] ;

    if ([StructIOS.use_url length] > 0) {
      StructIOS.array_url_modified = [StructIOS.input_url componentsSeparatedByString:@"/"];
      StructIOS.server = [StructIOS.array_url_modified  objectAtIndex:0];
      StructIOS.agent = [StructIOS.array_url_modified  objectAtIndex:1];
      StructIOS.roomid = [StructIOS.array_url_modified  objectAtIndex:2];

      NSLog(@"OK %@ %@ %@" ,
            StructIOS.server,
            StructIOS.agent,
            StructIOS.roomid);
    }
    else {
      NSLog(@"NOK");
    }
  }

  return 0;
}