简体   繁体   中英

iOS NSMutableArray initial custom object

How can I initial custom object ?

I have many Point data, so I create the Point data Object(class) like below:

MyPoint.h

 #import <Foundation/Foundation.h>

 @interface MyPoint : NSObject

 @property (assign, nonatomic) NSString*  pointName;
 @property (assign, nonatomic) NSString*  lat;
 @property (assign, nonatomic) NSString*  lng;

 @end

Then the data will composite many area, so I create other object in the MyArea.h

 #import "MyPoint.h"

 @interface MyArea : NSObject


@property (assign, nonatomic) NSString*  areaName;

 @property(strong,nonatomic) NSMutableArray<MyPoint*> *myPoint; // --1

 @end

How to initial the myPoint in my MyArea.m ?

I not sure which declare is right in below:

 @property(strong,nonatomic) NSMutableArray<MyPoint*> *myPoint;

or

 @property(strong,nonatomic) NSMutableArray<MyPoint> *myPoint;

And How can I create the NSMutableArray with my custom object(MyPoint) in the .m file.

In the MyArea.m

 #import "MyArea.h"

 @implementation MyArea

 -(id) init{
     self = [super init];
     if (self) {
         self.myPoint = [NSMutableArray<MyPoint*> new ];  // ??
   }
     return self;
 }
 @end

Have any one can teach me how to create the model in the objective-c.

I will get a lot of data then set in to the MyArea object.

Thank you very much.

You need to write a custom initWithPoints:(NSMutableArray)points.

For example:

- (instanceType)initWithPoints:(NSMutableArray<MyPoint *> *)points
{
    self = [super init];
    if(self) {
        NSLog(@"_init: %@", self);
        self.myPoint = points
    }
    return self;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM