nhlinh
nhlinh

Reputation: 121

How to init array of objects in objective-c, similar to swift

I am new iOS developer. I started to learn Swift but I want to switch to Objective-C. Below is the code which i'm using in Swift

I have Player.swift:

import Foundation
import UIKit

struct Player {
    var name: String!
    var game: String!
    var rating: Int

    init(name:String?, game:String?, rating:Int) {
        self.name = name
        self.game = game
        self.rating = rating
    }
}

And I have one class store data Player objects data and this data is using everywhere in project:

import Foundation
let playerData = [
    Player(name: "Bill Evan", game: "call of duty", rating: 4),
    Player(name: "Linh Nguyen", game: "Alien vs predator", rating: 3)
]

My question is how to do it in Objective-C:

I'm trying to do this in Player.h:

#import <Foundation/Foundation.h>

@interface Player : NSObject

@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *game;
@property int rating;

-(id)initWithPlayer:(NSString *)name
               game:(NSString*)game
             rating:(int)rat;

@end

And in Player.m:

#import "Player.h"

@implementation Player

-(id)initWithPlayer:(NSString *)name game:(NSString *)game rating:(int)rating {
    self.name = name;
    self.game = game;
    self.rating = rating;
    return self;
}
@end

How to create array of Player object store many players object?

Upvotes: 3

Views: 7054

Answers (2)

MarkHim
MarkHim

Reputation: 5766

Recommended:

Use a typed array NSArray<Player *> so that you don't have to cast the element when accessing it and the compiler warns you if you try to insert non-Player objects.

NSArray<Player *> *players = @[
    [[Player alloc] initWithPlayer:@"Steven" game:@"Pokémon Go" rating:100],
    [[Player alloc] initWithPlayer:@"Mike" game:@"Pokémon Silver" rating:90]
];

Player *steven = players[0];
steven.rating = steven.rating + 1;

Not recommended:

If you just do

NSArray *players = @[ ... see above ... ];

You would have to cast like this

Player *steven = (Player *)players[0];

Also this is more dangerous, since the compiler would also allow you to do this:

NSArray *players = @[
    [[Player alloc] initWithPlayer:@"Steven" game:@"Pokémon Go" rating:100],
    @"Just a String, not a Player object"
];

This could obviously result in a runtime crash when you're casting the @"Just a String, ..." object (players[1]) to a Player object.

Upvotes: 4

Clarence Leung
Clarence Leung

Reputation: 2556

To answer the question, you can initialize arrays of objects in Objective-C / Foundation with either one of two methods:

// Original, longer way of doing it
NSArray *words = [NSArray arrayWithObjects:@"list", @"of", @"words"];

// Modern Objective-C shorthand
NSArray *words = @[@"list", @"of", @"words"];

A little bit more verbose, but given your object, here's how your Swift code:

let playerData = [
    Player(name: "Bill Evan", game: "Call of Duty", rating: 4),
    Player(name: "Linh Nguyen", game: "Alien vs. Predator", rating: 3)
]

Would look in Objective-C:

NSArray *playerData = @[
    [[Player alloc] initWithPlayer:@"Bill Evan" game:@"Call of Duty" rating:4],
    [[Player alloc] initWithPlayer:@"Linh Nguyen" game:@"Alien vs. Predator" rating:3]
];

Upvotes: 2

Related Questions