Reputation: 6723
I have a class with init a build method that i want to use to create instances of class.
Code is the following
class Article {
let id:Int
let title:String
let subtitle:String
let editor1:String
let mainImage:NSData
init(id:Int, title:String, subtitle:String, editor1:String, mainImage:NSData) {
self.id = id
self.title = title
self.subtitle = subtitle
self.editor1 = editor1
self.mainImage = mainImage
}
class func build(json:JSON) -> Article {
id = Int(json["id"].string),
title = json["title"].string,
subtitle = json["subtitle"].string,
editor1 = json["editor1"].string,
mainImage = json["images"]["main"].rawData() {
return Article(
id: id,
title: title,
subtitle: subtitle,
editor1: editor1,
mainImage: mainImage)
}
}
}
But i have errors
What am I doing wrong ?
Upvotes: 0
Views: 520
Reputation: 70098
SwiftyJSON's .rawData()
is an Optional getter.
So I guess what you wanted to do is use if let
:
class func build(json:JSON) -> Article? {
id = Int(json["id"].string)
title = json["title"].string
subtitle = json["subtitle"].string
editor1 = json["editor1"].string
if let mainImage = json["images"]["main"].rawData() {
return Article(
id: id,
title: title,
subtitle: subtitle,
editor1: editor1,
mainImage: mainImage)
} else {
// ...
return nil
}
}
Also it looks like that you copied/pasted the parameters from your Article initializer to declare them earlier in the function but you forgot to get rid of the commas at the end of the lines.
Update
Your problem is that your class properties are immutable (declared with let
) but inside this function you are trying to change their values:
id = Int(json["id"].string)
This is interpreted as
self.id = Int(json["id"].string)
And you can't change the value of self.id
because it is immutable.
Solutions:
1- Make the properties mutable by using var
instead of let
. Example:
var id:Int
var title:String
var subtitle:String
var editor1:String
var mainImage:NSData
or
2- Do not replace the properties in the function since you're going to init with your new object anyway. Example:
class func build(json:JSON) -> Article? {
if let img = json["images"]["main"].rawData() {
return Article(
id: Int(json["id"].string),
title: json["title"].string,
subtitle: json["subtitle"].string,
editor1: json["editor1"].string,
mainImage: img)
} else {
// ...
return nil
}
}
Update 2
If the compiler complains about "not marked with try", do the "if let" with "try?":
if let img = try? json["images"]["main"].rawData() {
Explanation: SwiftyJSON may have changed this method without updating the documentation yet (or I didn't find it). It previously returned an Optional and now seems to "throw" instead. Using "try?" lets you make it an Optional again.
Upvotes: 1
Reputation: 981
You have ,s where they shouldn't be. Try using this:
class func build(json:JSON) -> Article {
id = Int(json["id"].string)
title = json["title"].string
subtitle = json["subtitle"].string
editor1 = json["editor1"].string
mainImage = json["images"]["main"].rawData() {
return Article(
id: id,
title: title,
subtitle: subtitle,
editor1: editor1,
mainImage: mainImage)
}
}
Upvotes: 0
Reputation: 5654
Couple things look off.
Article?
since there is a chance you're going to be returning nil.id = Int(json["id...
. Get rid of them.Upvotes: 1