(I am referring to the Swift code only.)
You can declare a property as
public private(set) var name: String?
to make it public read-only, but internally read-write. That makes
the additional accessor getName() obsolete.
If the property is only assigned in the init method and never changed later, you can simply make it constant:
public let name: String?
Since the initializer ensures that both name and address are assigned a value, you don't have to declare them as optionals.
The currentUser(), setCurrentUser(), removeCurrentUser()
methods can be replaced by a static property
public static var currentUser: User?
if you make the initializer public. static means class final,
i.e. it is a property of the type (and not an instance) and cannot
be overridden in subclasses.
The two guard statements can be combined into one.
The class then looks like this:
public class User {
public static var currentUser: User?
public let name: String
public let address: String
public init?(dictionary: [String : AnyObject]) {
guard let name = dictionary["name"] as? String,
let address = dictionary["address"] as? String
else { return nil }
self.name = name
self.address = address
}
}
The "current user" can now be set, retrieved, and unset like this:
User.currentUser = User(dictionary: ...)
if let user = User.currentUser {
// ...
}
User.currentUser = nil
If you change the initializer to
public init?(dictionary: [String : Any])
then you can pass a native Swift dictionary without casting, e.g.
User.currentUser = User(dictionary: ["name": "Joe", "address": "San Francisco"])