3

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?

2
  • Are you asking for the syntax? Commented Oct 6, 2016 at 9:46
  • You should use instancetype in init methods instead of id Commented Oct 6, 2016 at 9:52

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

oh, thank you so much @MarkHim. i'm just beginer in ios development :)
One thing, if i create class that store this Array and using it in everywhere in my app. How about this class ?. thank you !
I'm not sure if i understand correctly, but with #import you can import class headers to other objective c files. Simply Do #import "Players.h" in any other objective-c .h or .m file
This answer is neat.
@MarkHim so sorry, my wrong question. :) thank you !
2

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]
];

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.