简体   繁体   中英

Add objects in a NSMutableArray and use it multiple times in my project (Objective C)

I am new to Objective C, I'm trying to add objects to NSMutableArray and use it multiple times in my project. I have a Model class:

MainTarget.h

@interface Target1 : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) NSString *value;



- (instancetype) initWithTarget1: (NSString *)name andurl: (NSString *)url andvalue: (NSString *)value;



@end

MainTarget.m

#import <Foundation/Foundation.h>

#import "MainTarget.h"

@implementation Target1

- (instancetype) initWithTarget1: (NSString *)name andurl: (NSString *)url andvalue: (NSString *)value {

    self = [super init];

    if (self) {
        self.name = name;
        self.url = url;
        self.value = value;

    }
    return self;
}


@end

I get the results of my json in ViewController.m, and then create a object of Target1 and set Variables.

for (NSDictionary *targetparameters in parsedJSONArray) {
 Target1 *target = [[Target1 alloc]init];

  target.name = targetparameters[@"name"];
  target.url = targetparameters[@"url"];
  target.value = targetparameters[@"value"];
}

This is my json api.

[{"id":"1","cat_id":"0","id_user":"0","id_point":"1","name":"","phone":"","url":"http:\/\/rsm.tours\/api\/folder\/ar\/aaaa19-07-09-5-13-38.png","value":"http:\/\/rsm.tours\/api\/folder\/ar\/aaaa19-07-09-5-13-38.mp4","state":"1"},{"id":"2","cat_id":"0","id_user":"0","id_point":"1","name":"","phone":"","url":"https:\/\/i.pinimg.com\/736x\/f9\/24\/43\/f92443e14d3e08d773881aef156e55c2.jpg","value":"https:\/\/hw20.cdn.asset.aparat.com\/aparat-video\/38888e829661bc305f7944b4f930c15515677561-144p__46889.mp4","state":"1"}]

I want to add target in a NSMutableArray and use it in a function (initialize) of myAR.m with a for loop.

myAr.m

BOOL initialize()
{
      // for loop
}

If you working with storyboards and segues, follow these steps.

  1. After creating each Target1, just add it in a NSMutableArray variable like that:

     NSMutableArray *targetArray = [NSMutableArray array]; for (NSDictionary *targetparameters in parsedJSONArray) { Target1 *target = [[Target1 alloc]init]; target.name = targetparameters[@"name"]; target.url = targetparameters[@"url"]; target.value = targetparameters[@"value"]; [targetArray addObject:target]; } // step 3.1
  2. So, in each ViewController as you need the targetArray, prepare a property to received it.

     @interface ViewController2: UIViewController @property (nonatomic, strong) NSMutableArray *targetArrayVC2;
  3. Finally, in ViewController1, pass the targetArray via performSegue and prepareForSegue:segue:sender delegate.

     // 3.1 [self performSegueWithIdentifier:@"segueViewController2" sender: targetArray]; - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"segueViewController2"]){ ViewController2 *viewController2 = (ViewController2 *)[segue destinationViewController]; [viewController2 setTargetArrayVC2:sender]; // sender = targetArray } }

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