m9st m9st - 5 months ago 11x
Objective-C Question

Objective - C -> Swift

please help I'm new in swift and i can't "translate" code from obj-c to swift. Some literature or any help, please. Or any analogies to INSTANCETYPE in swift

@implementation Message

+ (instancetype)messageWithString:(NSString *)message
return [Message messageWithString:message image:nil];

+ (instancetype)messageWithString:(NSString *)message image:(UIImage *)image
return [[Message alloc] initWithString:message image:image];

- (instancetype)initWithString:(NSString *)message
return [self initWithString:message image:nil];

- (instancetype)initWithString:(NSString *)message image:(UIImage *)image
self = [super init];
_message = message;
_avatar = image;
return self;


Screen of code


The convention in Swift is you don't translate the factory methods (look at how Apple translate NSString).

Try this:

class Message: NameOfSuperClass {
    var message: String
    var image: UIImage?

    init(message: String, image: UIImage? = nil) {
        self.message = message
        self.image = image


let a = Message(message: "Hello world")
let b = Message(message: "Goodbye world", image: anImage) // Assume anImage is defined