0

I have some array( @private NSArray employees []; ) and method , which take string parameter( name of employees) and put this parameter in array. How I can do this with Objective-c?

2 Answers 2

1

You could do the following,

Create a property,

@property (nonatomic) NSMutableArray *array;

Initialise the array in your init method or somewhere else appropriate,

self.array = [[NSMutableArray alloc] init];

Your method could look something like this,

- (void)addEmployeeName:(NSString *)employeeName {
   [self.array addObject:employeeName];
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can create a class Employee and use a type with your array. For example:

NSArray<Employee*> * employees = [[NSArray alloc] init];

At this point, your method will be:

-(Employee *)createEmployee:(NSString *)name{
   Employee *myEmpl = [[Employee alloc]init];
   [myEmpl setName:name];
   return myEmpl;
}

and you can add the object (of type Employee) in your array in this way:

[employees addObject:[self createEmployee]];

the same thing is possibile with an object of type NSString instead of Employee. You can also avoid defining the type in your NSArray because Objective-C use the type inference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.