Swift Cannot assign to self in a method

Olexiy Pyvovarov

I've just faced a strange problem

I have a class called Letter

class Letter
{    
    init() {}
}

And I have an extension for this class:

extension Letter
{
    convenience init(file_path:String) { self = Letter.loadFromFile(file_path)}
    class func loadFromFile(file_path:String)->Letter {...}
}

I need to create and init with path to file and when i call Letter(file_path) I need a new object that returned by a func loadFromFile. How to assign in an init method or to return a new object?

//Edit The same problem...

Andrew

Class functions that return instances of that class seems to be an anti-pattern in Swift. You'll notice that the "with" Objective-C class methods like [NSString stringWithString:@"some other string"] made the transition as "with"-less convenience initializers: NSString(string: "some other string").

Furthermore, you'll need to delegate to a designated initializer from within a convenience initializer.

Also, since you're 1) defining the original class and 2) don't need the convenience initializer scoped differently than the designated initializer, I don't see any reason to place it in an extension.

Putting those together:

class Letter {
    init() { … }

    convenience init(filePath: String) {
        self.init()
        loadFromFile(filePath)
    }

    func loadFromFile(filePath: String) { … }
}

let letter1 = Letter()
letter1.loadFromFile("path1")

let letter2 = Letter(filePath: "path2")

In summary, the analogy for assigning to self in Swift is calling an initializer.

Let me know if this works for you!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot assign to self out of a method in the init family?

Cannot assign to "" in "self" - Working with Tuples in Swift

Cannot assign to property: 'self' is immutable swift error

Swift: Assign a class method to a handler and handling weak self

Cannot assign to property: 'self' is immutable

Swift - Assign a NIB to self in class

Swift - self in deinit method

Cannot assign to `self.x` because it is borrowed

Swiftui Cannot assign to property: 'self' is immutable Error

Cannot assign property in method of struct

Cannot assign method to variable php

cannot assign through subscript in swift

Cannot assign to GetDropDownValues because it is a method group?

How to fix 'cannot assign to Partial<this>' in subclass method?

Cannot assign date using GetDateTime method of IDataReader

Cannot assign to 'writeline' because it is a 'method group'

Cannot Assign to class because it is a method group

Unexpected Cannot assign to property 'self' is immutable compile time error IN CLASS

Cannot assign to property: 'self' is immutable while assigning value to @ObservedObject

Cannot assign this CGFloat to a property in XCode using Swift

Cannot Assign the variables of a function to the Labels on Swift

Cannot assign value type mismatch swift

Cannot assign value, Swift treat is as immutable

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift

Swift error: Cannot assign to immutable value

Swift Cannot Assign to immutable value of type NSData

Swift: Cannot assign to immutable expression of type 'AnyObject?!'

Cannot assign to value: 'i' is a 'let' constant in swift

Cannot assign sorted array to a variable in Swift