【问题标题】:How to process an object into an NSMutableArray如何将对象处理成 NSMutableArray
【发布时间】:2012-07-06 15:05:33
【问题描述】:

我有一个这样的对象:

@interface object : NSObject
{    
    NSString *_observation;    
    NSInteger _id;    
    NSString *_date;
    NSString *_device;  
    double _latitude;
    double _longitude; 
}

所以你看到它有一些属性。然后我创建了一个NSMutableArray,里面装满了上面那个对象,比如

array[object, object ,object, ....]

我想问的是如何将特定对象的特定属性处理到数组中? 比如如何将第一个的对象id属性处理成obj-c中的数组?

非常感谢!

【问题讨论】:

  • “将属性处理到数组中”是什么意思?你想在数组中插入一个属性吗?或者检索数组中对象的属性?顺便说一句,您需要围绕这些变量创建一些属性,否则您将无法从 object 类之外访问它们。
  • 检索数组对象的属性。当然我已经创建了属性,这只是一个伪代码示例
  • 很难理解你在问什么,你能举例说明你想做的事情吗?
  • 真的吗???我的英语这么差吗?我想要例如这个:array.firstObject.id 我怎么能引用这个东西?

标签: objective-c ios nsmutablearray nsobject


【解决方案1】:

如果我正确理解了这个问题,您希望在将对象的实例变量放入 NSMutableArray 后访问它。

如果问题这么简单,这就是您的解决方案:

NSInteger *currID = [array objectAtIndex:x].id;

此外,您可以使用快速枚举来遍历整个数组,访问每个元素的变量。

NSInteger *currID;
for (NSObject *currentObject in array) {
    currID = currentObject.id;
    //proccess ID
}

这假设您已经在 .h 文件中为id 设置了@property

@property (nonatomic) BOOL latitude;
@property (nonatomic) BOOL longitude;
@property (nonatomic, copy) NSString *observation;    
@property (nonatomic) NSInteger id;    
@property (nonatomic, copy) NSString *date;
@property (nonatomic, copy) NSString *device;

当然还有 .m:

@synthesize latitude;
@synthesize longitude;
@synthesize observation;
@synthesize id;
@synthesize date;
@synthesize device;

【讨论】:

  • 如果_id 未声明为属性,您将无法使用. 访问它。
  • 实例变量默认受保护,所以你不能按照你的建议去做。请修改您的答案。
  • @Flex_Addicted 公平评估。
【解决方案2】:

为了访问数组中对象的成员,您需要获取指向该对象的指针。即:

object *firstObject = [array objectAtIndex:0];
firstObject.id = 0;
object *secondObject = [array objectAtIndex:1;
...

【讨论】:

    【解决方案3】:

    要么

    [[array objectAtIndex:0] id]
    

    [array objectAtIndex:0].id
    

    假设_id 数据成员周围的属性称为id。这是一个属性 getter 调用,而不是直接的 ivar 访问。

    NSArray 不能也不能用它们的 a[i] 索引符号来模拟 C 数组。这不是 C++,其中 [] 运算符可以重新定义。在上面的行中,括号表示 ObjC 方法调用(也称为“消息传递”),而不是数组索引。

    【讨论】:

      【解决方案4】:

      NSArray 文档是你的朋友。

      无论如何,要访问对象数组,您可以使用NSArray- (id)objectAtIndex:(NSUInteger)index 方法。

      Seva Alekseyev 的意思是,您的班级必须以不同的方式组织。例如:

      //.h
      @interface MyCustomObject : NSObject
      {
          // you dont need to declare variables, the compiler will do it for you
      }
      
      @property (nonatomic, copy) NSString* objObservation;
      @property (nonatomic, assign) NSInteger objId;
      @property (nonatomic, copy) NSString* objDate;
      @property (nonatomic, copy) NSString* objDevice;
      @property (nonatomic, assign) double objLatitude;
      @property (nonatomic, assign) double objLongitude;
      
      @end
      
      //.m
      @implementation MyCustomObject
      
      @synthesize objId;
      @synthesize objDate;
      @synthesize objDevice;
      @synthesize objLatitude;
      @synthesize objLongitude;
      @synthesize objObservation;
      
      - (void)dealloc
      {
          /* uncomment if you don't use ARC
          [objDate release];
          [objDevice release];
          [objObservation release];
      
          [super dealloc];
           */
      }
      
      @end
      

      像这样使用你的类(你需要#import "MyCustomObject.h"

      // populate your object
      MyCustomObject* myObj = [[MyCustomObject alloc] init];
      myObj.objDate = @"yourDate";
      myObj.objDevice = @"yourDevice";
      myObj.objId = 1;
      myObj.objLatitude = 22.0;
      myObj.objLongitude = 23.87;
      myObj.objObservation = @"yourObservation";
      
      // insert it into your array (here only for test purposes but you could create an instance variable for store it)
      NSArray* myArray = [NSArray arrayWithObjects:myObj, nil];
      
      // if you don't use ARC
      [myObj release];
      
      // grab the object from the array
      MyCustomObject * currentObj = [myArray objectAtIndex:0];
      // see its values, for example
      NSLog(@"%@", [currentObj objDevice]);
      

      现在我要添加一些注释。

      • 当你使用类时,用大写字母命名它们,例如MyClass 而不是 myclass,变量以小写字母开头,例如myVariable
      • 如果您的数组可以在不同时间填充,您需要一个NSMutableArrayNSArray 一旦创建就无法更改。 NSMutableArray 是动态的,您可以向其添加或删除对象
      • 当你处理对象时,你应该用属性包装你的变量(在这种情况下你不需要变量,因为编译器会为它们提供@synthesize 指令)。这允许您不破坏对象封装
      • 如果你愿意,你可以考虑为你的对象提供一个初始化器

      当你写问题时,试着在它周围加上一些背景,并试着解释你做了什么以及你想要实现什么。

      【讨论】:

      • 在这种情况下,您不想使用strongretain 而不是copy。这只是额外的开销,因为它们无论如何都不是可变的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多