swift 4 model class error: Return from initializer without initializing all stored properties - swift

I created a model class for notes that have the attributes title and content. I am getting an error in the second init that says: Return from initializer without initializing all stored properties. Im feel like there is something I am missing and I cant seem to see it. This is the code:
class Note{
private var _title: String
private var _content: String
var title: String!{
return self._title
}
var content: String!{
return self._content
}
init(title: String, content: String){
self._title = title
self._content = content
}
init(noteData: Dictionary<String, AnyObject>) {
if let title = noteData["title"] as? String {
self._title = title
}
if let content = noteData["content"] as? String {
self._content = content
}
}
}

You have to initialise all properties of your class in its initializer. Regarding your dictionary initializer you should make it fallible and return nil in case of missing key/value pairs:
class Note {
let title: String
let content: String
init(title: String, content: String) {
self.title = title
self.content = content
}
init?(dictionary: [String: Any]) {
guard
let title = dictionary["title"] as? String,
let content = dictionary["content"] as? String
else { return nil }
self.title = title
self.content = content
}
}
Note that if use a struct instead of a class you don't even have to implement an initializer for your custom object:
struct Note {
let title: String
let content: String
init?(dictionary: [String: Any]) {
guard
let title = dictionary["title"] as? String,
let content = dictionary["content"] as? String
else { return nil }
self.title = title
self.content = content
}
}

The problem is that in the second init, if the unwrap fails the properties will not be set. For the init to work correctly the properties that are not optional will all need values. The best way to fix this is to make the properties optional.
Optional properties:
private var title: String?
private var content: String?
By making the properties optional you can avoid doing the if let in the init:
self.title = noteDate["title"]
Make sure to unwrap safely when you use the properties though!

Related

Using a designated initialiser VS a static method to populate a struct

I have been working on a struct that parses a JSON data, that data is from Dark sky. While I was working on it, something hit my brain and been pondering on it ever since.
Traditionally, we have been using designated initializers (although struct gives you a member-wise initializer for free) to instantiate an object. But, we could easily use a static function that returns itself and that function populates the properties.
like so:
struct WeatherForecastData {
// Weather data
var apparentTemperature: Double?
var icon: String?
var precipProbability: Double?
var pressure: Double?
static func map(_ data: [String: AnyObject]) -> WeatherForecastData {
var p = WeatherForecastData()
p.apparentTemperature = data["apparentTemperature"] as? Double ?? nil
p.icon = data["icon"] as? String ?? nil
p.precipProbability = data["precipProbability"] as? Double ?? nil
p.pressure = data["pressure"] as? Double ?? nil
return p
}
}
notice the static method, if we replace it with a designated initializer, it would be doing the same thing as a static method. My question is when should we use static methods to instantiate an object instead of the traditional designated initializer?
Why wouldn't you just use a custom initializer like this?
struct WeatherForecastData {
var apparentTemperature: Double
var icon: String
var precipProbability: Double
var pressure: Double
init?(data:Dictionary<String,Any>) {
guard
let apparentTemperature = data["apparentTemperature"] as? Double,
let icon = data["icon"] as? String,
let precipProbability = data["precipProbability"] as? Double,
let pressure = data["pressure"] as? Double
else {
print("Invalid Data")
return nil
}
self.apparentTemperature = apparentTemperature
self.icon = icon
self.precipProbability = precipProbability
self.pressure = pressure
}
}

Swift 3 optional parameters

Is it possible to create optional initialization parameters in Swift so I can create an object from JSON with the values returned from the API call, but then when I'm saving that object later I can also save the downloaded UIImage for one of the urls I got before.
Example:
class Story: NSObject, NSCoding {
var id: Int?
var title, coverImageURL: String?
var coverImage: UIImage?
required init?(anId: Int?, aTitle: String?, aCoverImageURL: String?) {
self.id = anId
self.title = aTitle
self.coverImageURL = aCoverImageURL
}
convenience init?(json: [String: Any]) {
let id = json["id"] as? Int
let title = json["title"] as? String
let coverImageURL = json["cover_image"] as? String
self.init(
anId: id,
aTitle: title,
aCoverImageURL: coverImageURL,
)
}
Then Later I want to save objects to memory
//MARK: Types
struct PropertyKey {
static let id = "id"
static let title = "title"
static let coverImageURL = "coverImageURL"
static let coverImage = "coverImage"
}
//MARK: NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(id, forKey: PropertyKey.id)
aCoder.encode(title, forKey: PropertyKey.title)
aCoder.encode(coverImageURL, forKey: PropertyKey.coverImageURL)
aCoder.encode(coverImage, forKey: PropertyKey.coverImage)
}
required convenience init?(coder aDecoder: NSCoder) {
guard let id = aDecoder.decodeObject(forKey: PropertyKey.id) as? Int else {
os_log("Unable to decode the id for a Story object.", log: OSLog.default, type: .debug)
return nil
}
guard let title = aDecoder.decodeObject(forKey: PropertyKey.title) as? String else {
os_log("Unable to decode the title for a Story object.", log: OSLog.default, type: .debug)
return nil
}
let coverImageURL = aDecoder.decodeObject(forKey: PropertyKey.coverImageURL) as? String
let coverImage = aDecoder.decodeObject(forKey: PropertyKey.coverImage) as? UIImage
self.init(
anId: id,
aTitle: title,
aCoverImageURL: coverImageURL,
coverImage: coverImage,
)
}
Does this make sense? I want to be able to save a Story object as soon as I get the response from the API, but later when I save the story to memory, I want to be able to save the fetched UIImage for the coverImage.
How would I do that?
I'm not sure why no one took the easy points on this answer, but the answer is to simply make your properties optionals, and then you can set them with a value, or nil. You can also create convenience initializers that automatically set certain values to nil if you want. So, using my app as an example, I have a model that gets built from an API call. that model has values like id, created_at, etc that don't exist until a record is saved to the server, but I create objects locally, store them, and eventually send them to the server, so I need to be able to set the above values only when creating an object from JSON, so here is what I did:
class Story: NSObject, NSCoding {
var id: Int?
var title, coverImageURL: String?
var coverImage: UIImage?
required init?(anId: Int?, aTitle: String?, aCoverImageURL: String?) {
self.id = anId
self.title = aTitle
self.coverImageURL = aCoverImageURL
}
convenience init?(json: [String: Any]) {
let id = json["id"] as? Int
let title = json["title"] as? String
let coverImageURL = json["cover_image"] as? String
self.init(
anId: id,
aTitle: title,
aCoverImageURL: coverImageURL,
)
}
convenience init?(aTitle: String, aCoverImage: UIImage?) {
let title = aTitle
let subtitle = aSubtitle
let coverImage = aCoverImage
let isActive = activeStatus
self.init(
anId: nil,
aTitle: title,
aCoverImageURL: nil,
aCoverImage: coverImage,
)
}
As you can see, I only set two of the values when I'm creating an object locally, and the other values are just set to nil. To allow a value to be set to nil, just make it an optional when setting it. Simple!

Swift 2.2 singleton

I am new in Swift. I am trying to parse some JSON data from web service and want a singleton class of user.But I got stuck to create the singleton. Here is my code:
import Foundation
class User {
private var success: String
private var userId: String
private var name: String
private var gender: String
private var email: String
private var userObject = [User]()
class var sharedInstane:User {
struct Singleton {
static var onceToken: dispatch_once_t = 0
static var instance:User? = nil
}
dispatch_once(&Singleton.onceToken){
Singleton.instance = User()
}
return Singleton.instance!
}
private init(success: String, userId: String, name: String, gender: String, email: String)
{
self.success = success
self.userId = userId
self.name = name
self.gender = gender
self.email = email
}
convenience init(dictionary: [String:AnyObject]) {
let success = dictionary["success"] as? String
let userId = dictionary["userId"] as? String
let name = dictionary["name"] as? String
let gender = dictionary["gender"] as? String
let email = dictionary["email"] as? String
self.init(success: success!, userId: userId!, name: name!, gender: gender!, email: email!, )
}
func callWebserviceToLoadUserInfo (url:String, param:[String:AnyObject],completeHandler:(Bool?,String) -> ())
{
let connection = ServerConnection()
connection.getJSONDataForWebService(url, params: param) { (response, error) in
// code goes here
var responseDict = response as! [String : AnyObject]
responseDict = responseDict["responseDict"] as! [String : AnyObject]
if responseDict["success"] as! String == "1" {
for dict in responseDict {
let user = User(dictionary: (dict as! [String:AnyObject]))
self.userObject.append(user)
}
print("user : \(self.userObject[0].name)")
}else{
// error goes here
}
}
}
}
Can any one please help me how should I do this code?
The singleton in the single line sample code.
class TheOneAndOnlyKraken {
static let sharedInstance = TheOneAndOnlyKraken()
private init() {} //This prevents others from using the default '()' initializer for this class.
}
For more details.
Using Krakendev's single-line singleton code, cited by Maheshwar, and turning your convenience init into an instance function to be called with User.sharedInstance.initialize(dictionary):
import Foundation
class User {
// Here you declare all your properties
// "private var" and all that jazz
static let sharedInstance = User()
private init() {
// If you have something to do at the initialization stage
// you can add it here, as long as it does not involve
// arbitrary values that you would pass as parameters.
}
func initialize(dictionary: [String:AnyObject]) {
// Transfer the values of the dictionary to each `self.property`.
// Be careful while using `as?` as you may have to deal with
// optionals. No need to call `self.init` at the end, because
// this is now a regular `func`.
}
// Add the rest of your stuff here
}
One note about how you were working inside of that convenience initializer: if you do property = SomeClass.someMethod().someProperty as? SomeType, then property will be of type SomeType?, or Optional(SomeType). According to The Swift Programming Language,
The conditional form, as?, returns an optional value of the type you are trying to downcast to.
While User was not instantiated at least one time sharedInstance will return nil. After the first successful instantiation of the User, sharedInstance starts return it and that's became impossible to instantiate another one User as singleton pattern requires it. Consider this:
class User {
private static var sharedUser: User?
class var sharedInstance: User? {
return sharedUser
}
private init(success: String, userId: String, name: String, gender: String, email: String)
{
//User initialization code here
User.sharedUser = self
}
convenience init?(dictionary: [String:AnyObject]) {
guard User.sharedUser == nil else {
return nil
}
//dictionary parsing code is here
self.init(success: success!, userId: userId!, name: name!, gender: gender!, email: email!)
}
}
Client's code:
User.sharedUser
//return nil
let dict: [String:AnyObject] = ["success": "success", "userId":"userId", "name":"name", "gender":"gender","email":"email"]
User(dictionary: dict)
//creates User
User.sharedUser
//returns just created user
User(dictionary: dict)
//return nil
You should think about making this two classes, so that User is your model class and then create a manager to handle all the users (which seems to be your goal).
So in User remove the sharedInstane part and create a second singleton class, e.g. called UserManager, with the standard way to create a singleton in Swift. Then you can keep the way you're creating your user and in the end just assign it to the singleton:
class UserManager {
static let sharedInstance = UserManager()
var users = [User]()
}
// in your code:
...
for dict in responseDict {
let user = User(dictionary: (dict as! [String:AnyObject]))
UserManager.sharedInstance.users.append(user)
}
...

Redesign Init (MKAnnotation)

class Point: NSObject, MKAnnotation {
var id: String?
var title: String?
var coordinate: CLLocationCoordinate2D
var subtitle: String?
init(id: String, dictionary: Dictionary<String, AnyObject>){
self.id = id
if let title = dictionary["title"] as? String {
self.title = title
}
if let subtitle = dictionary["subtitle"] as? String {
self.subtitle = subtitle
}
if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
self.coordinate.latitude = coords.values.first!["latitude"]!
self.coordinate.longitude = coords.values.first!["longitude"]!
}
super.init()
}
/*
init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D){
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
}*/
}
I implemented initializer for creating Point on Map. I must redesign init because I use Firebase and results from database are in dictionary.
It writes: Property self.coordinate not initialized at implicitly generated super.init call
init(id: String, dictionary: Dictionary<String, AnyObject>){
self.id = id
if let title = dictionary["title"] as? String {
self.title = title
}
if let subtitle = dictionary["subtitle"] as? String {
self.subtitle = subtitle
}
if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
self.coordinate.latitude = coords.values.first!["latitude"]!
self.coordinate.longitude = coords.values.first!["longitude"]!
}
super.init()
}
So, the problem here mostly lies in the fact that you only ever initialize your coordinate property within an if. Because coordinate is not marked as an optional, it much be given a value before you call super.init(), as per the rules of Swift initializers.
And because you initialize it within an if only, there's a chance that the if fails and you never initialize it at all. This would leave the instance in a completely invalid state, which is not allowed per the rules of Swift initializers and fails the first safety check.
Safety check 1
A designated initializer must ensure that all of the properties
introduced by its class are initialized before it delegates up to a
superclass initializer.
You could make coordinate an optional instance variable, must like your other instance variables. However, I'd argue that probably makes most sense is that id & title are probably also non-optional, and all of these properties probably should be let constants.
As such, I'd rewrite your class and that initializer as such:
class Point: NSObject, MKAnnotation {
let id: String
let title: String?
let coordinate: CLLocationCoordinate2D
let subtitle: String?
init?(id: String, dictionary: [String: AnyObject]){
self.id = id
title = dictionary["title"] as? String
subtitle = dictionary["subtitle"] as? String
guard let
coordinates = dictionary["coordinates"] as? [String: [String: Double]],
latitude = coordinates.values.first?["latitude"],
longitude = coordinates.values.first?["longitude"] else {
return nil
}
coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
super.init()
}
}
Now you have a failable initializer. If any of the values in the dictionary is not correct, the initializer returns nil. Otherwise, your values are all properly initialized.

cannot return String in function

I am having trouble casting an option AnyObject into a string. Whenever I try to call the fuction my program crashes with (lldb). This is the function.
func name() -> String {
print(attributes["name"])
print(attributes["name"]! as! String)
let name = attributes["name"]! as! String
return name
}
The output from the prints is:
Optional(Optional(Josh))
Josh
(lldb)
Thanks in advance for your help!
Lets say attributes is defined as follow
var attributes: NSMutableDictionary? = NSMutableDictionary()
and can be populated like follow
attributes?.setValue("Walter White", forKey: "name")
Optionals
You should design the name() function to return a String or nil (aka String? which is an Optional type)
func name() -> String? {
guard let
attributes = attributes,
name = attributes["name"] as? String else { return nil }
return name
}
The same logic can also be written this way
func name() -> String? {
return attributes?["name"] as? String
}
Now if a valid String value is found inside attributes with key name then it is returned. Otherwise the function does return nil.
Invoking the function
When using the function you should unwrap the result like this
if let name = name() {
print(name) // prints "Walter White"
}
In all these examples, attributes is defined as:
var attributes: AnyObject? = ["name": "Josh"]
Looks like the crash occurs due to type-safety issues. Try:
func name() -> String? {
if let name = attributes!["name"] as? String {
return name
}
return nil
}
Another option, which is slightly swiftier:
func name() -> String? {
guard let name = attributes!["name"] as? String else { return nil }
return name
}
Yet another option that would be using a block for the function, so that it doesn't return anything if attributes doesn't contain a key "name":
func name(block: ((text: String?) -> Void)) {
guard let name = attributes!["name"] as? String else { return }
return block(text: name)
}
// Usage:
name { text in
print(text!)
}
Prints:
Josh
if let _string = attributes["name"] as? String {
return _string
}
// fallback to something else, or make the method signature String?
return ""
When working with optionals, you don't want to just wrap things with exclamation points. If the value ever ended up not being a string, or not being there at all in the map, you're code would fail hard and potentially crash your application.
If you need a non-optional String, consider returning an empty string as a fallback method and using the if let pattern to return the optional string if it is available.
-- EDIT --
Not sure about the downvote... Here it is in a playground.
var attributes = [String:AnyObject]()
attributes["name"] = "test"
func name() -> String {
print(attributes["name"])
print(attributes["name"]! as! String)
let name = attributes["name"]! as! String
return name
}
// does not compile
//print(name())
func name2() -> String {
if let _string = attributes["name"] as? String {
return _string
}
// fallback to something else, or make the method signature String?
return ""
}
// prints test
print(name2())