Error trying to decode custom NSCoding object in Swift [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
Swift 3 saving and retrieving custom object from userDefaults
3 answers
This is my code:
required convenience init?(coder aDecoder: NSCoder) {
// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
os_log("Unable to decode the name for a Savings object.", log: OSLog.default, type: .debug)
return nil
}
// Because photo is an optional property of Savings, just use conditional cast.
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
let savingsAmount = aDecoder.decodeObject(forKey: PropertyKey.savingsAmount)
let amountSavedPerWeek = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
let amountSavedSoFar = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
// Must call designated initializer.
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(savingsAmount, forKey: PropertyKey.savingsAmount)
aCoder.encode(amountPerWeek,forKey: PropertyKey.amountSavedPerWeek)
aCoder.encode(amountSavedSoFar, forKey: PropertyKey.amountSavedSoFar)
}
On this line:
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
properties
var name: String
var photo: UIImage?
var savingsAmount: String
var amountPerWeek: String
var amountSavedSoFar: Int
I get the error Cannot convert value of type 'Int.Type' to expected argument type 'Int'. I can't seem to figure the fix to this, the only time the error came up was when I added the type "Int". Thanks for the help.
swift
marked as duplicate by Ken White, Leo Dabus
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 2:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
Swift 3 saving and retrieving custom object from userDefaults
3 answers
This is my code:
required convenience init?(coder aDecoder: NSCoder) {
// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
os_log("Unable to decode the name for a Savings object.", log: OSLog.default, type: .debug)
return nil
}
// Because photo is an optional property of Savings, just use conditional cast.
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
let savingsAmount = aDecoder.decodeObject(forKey: PropertyKey.savingsAmount)
let amountSavedPerWeek = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
let amountSavedSoFar = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
// Must call designated initializer.
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(savingsAmount, forKey: PropertyKey.savingsAmount)
aCoder.encode(amountPerWeek,forKey: PropertyKey.amountSavedPerWeek)
aCoder.encode(amountSavedSoFar, forKey: PropertyKey.amountSavedSoFar)
}
On this line:
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
properties
var name: String
var photo: UIImage?
var savingsAmount: String
var amountPerWeek: String
var amountSavedSoFar: Int
I get the error Cannot convert value of type 'Int.Type' to expected argument type 'Int'. I can't seem to figure the fix to this, the only time the error came up was when I added the type "Int". Thanks for the help.
swift
marked as duplicate by Ken White, Leo Dabus
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 2:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
You need to pass anIntvalue, not the keywordInt.
– rmaddy
Nov 10 at 1:25
let amountSavedSoFar = aDecoder.decodeInteger(forKey: PropertyKey.amountSavedPerWeek)
– Leo Dabus
Nov 10 at 1:56
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: amountSavedSoFar)
– Leo Dabus
Nov 10 at 1:56
1
@LeoDabus got it to work with that! Thanks!
– Evan
Nov 10 at 2:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Swift 3 saving and retrieving custom object from userDefaults
3 answers
This is my code:
required convenience init?(coder aDecoder: NSCoder) {
// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
os_log("Unable to decode the name for a Savings object.", log: OSLog.default, type: .debug)
return nil
}
// Because photo is an optional property of Savings, just use conditional cast.
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
let savingsAmount = aDecoder.decodeObject(forKey: PropertyKey.savingsAmount)
let amountSavedPerWeek = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
let amountSavedSoFar = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
// Must call designated initializer.
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(savingsAmount, forKey: PropertyKey.savingsAmount)
aCoder.encode(amountPerWeek,forKey: PropertyKey.amountSavedPerWeek)
aCoder.encode(amountSavedSoFar, forKey: PropertyKey.amountSavedSoFar)
}
On this line:
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
properties
var name: String
var photo: UIImage?
var savingsAmount: String
var amountPerWeek: String
var amountSavedSoFar: Int
I get the error Cannot convert value of type 'Int.Type' to expected argument type 'Int'. I can't seem to figure the fix to this, the only time the error came up was when I added the type "Int". Thanks for the help.
swift
This question already has an answer here:
Swift 3 saving and retrieving custom object from userDefaults
3 answers
This is my code:
required convenience init?(coder aDecoder: NSCoder) {
// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
os_log("Unable to decode the name for a Savings object.", log: OSLog.default, type: .debug)
return nil
}
// Because photo is an optional property of Savings, just use conditional cast.
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
let savingsAmount = aDecoder.decodeObject(forKey: PropertyKey.savingsAmount)
let amountSavedPerWeek = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
let amountSavedSoFar = aDecoder.decodeObject(forKey: PropertyKey.amountSavedPerWeek)
// Must call designated initializer.
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(savingsAmount, forKey: PropertyKey.savingsAmount)
aCoder.encode(amountPerWeek,forKey: PropertyKey.amountSavedPerWeek)
aCoder.encode(amountSavedSoFar, forKey: PropertyKey.amountSavedSoFar)
}
On this line:
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: Int)
properties
var name: String
var photo: UIImage?
var savingsAmount: String
var amountPerWeek: String
var amountSavedSoFar: Int
I get the error Cannot convert value of type 'Int.Type' to expected argument type 'Int'. I can't seem to figure the fix to this, the only time the error came up was when I added the type "Int". Thanks for the help.
This question already has an answer here:
Swift 3 saving and retrieving custom object from userDefaults
3 answers
swift
swift
edited Nov 10 at 2:57
Leo Dabus
128k30260337
128k30260337
asked Nov 10 at 1:07
Evan
33
33
marked as duplicate by Ken White, Leo Dabus
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 2:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ken White, Leo Dabus
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 2:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
You need to pass anIntvalue, not the keywordInt.
– rmaddy
Nov 10 at 1:25
let amountSavedSoFar = aDecoder.decodeInteger(forKey: PropertyKey.amountSavedPerWeek)
– Leo Dabus
Nov 10 at 1:56
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: amountSavedSoFar)
– Leo Dabus
Nov 10 at 1:56
1
@LeoDabus got it to work with that! Thanks!
– Evan
Nov 10 at 2:04
add a comment |
2
You need to pass anIntvalue, not the keywordInt.
– rmaddy
Nov 10 at 1:25
let amountSavedSoFar = aDecoder.decodeInteger(forKey: PropertyKey.amountSavedPerWeek)
– Leo Dabus
Nov 10 at 1:56
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: amountSavedSoFar)
– Leo Dabus
Nov 10 at 1:56
1
@LeoDabus got it to work with that! Thanks!
– Evan
Nov 10 at 2:04
2
2
You need to pass an
Int value, not the keyword Int.– rmaddy
Nov 10 at 1:25
You need to pass an
Int value, not the keyword Int.– rmaddy
Nov 10 at 1:25
let amountSavedSoFar = aDecoder.decodeInteger(forKey: PropertyKey.amountSavedPerWeek)– Leo Dabus
Nov 10 at 1:56
let amountSavedSoFar = aDecoder.decodeInteger(forKey: PropertyKey.amountSavedPerWeek)– Leo Dabus
Nov 10 at 1:56
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: amountSavedSoFar)– Leo Dabus
Nov 10 at 1:56
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: amountSavedSoFar)– Leo Dabus
Nov 10 at 1:56
1
1
@LeoDabus got it to work with that! Thanks!
– Evan
Nov 10 at 2:04
@LeoDabus got it to work with that! Thanks!
– Evan
Nov 10 at 2:04
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You have this (I added line breaks to make it easier to read):
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: Int)
but I think you meant to type this:
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: amountSavedSoFar as! Int)
Basically, you forgot to pass in the local variable amountSavedSoFar and instead just typed Int, so the compiler thinks you're trying to pass in the type Int itself as the parameter, which is why you're getting that error.
Also, one unrelated thing, I find it interesting that if name is not able to be decoded, then you return nil, but then if savingsAmount, amountPerWeek, or amountSavedSoFar cannot be decoded then you've opted to crash the app by using the "crash here" operator, as!. You may also want to return nil if any of those can't be decoded either.
Ok... Umm I tried that, but when I do that it gives me more errors. 🙄: Expected expression in list of expressions Cannot invoke 'Savings.init' with an argument list of type '(name: String, photo: UIImage?, savingsAmount: String, amountPerWeek: String)'
– Evan
Nov 10 at 1:57
@Evan Ah, I failed to notice that you needed to usedecodeIntegerinstead ofdecodeObject. Glad you got it to work at least!
– TylerTheCompiler
Nov 10 at 4:34
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You have this (I added line breaks to make it easier to read):
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: Int)
but I think you meant to type this:
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: amountSavedSoFar as! Int)
Basically, you forgot to pass in the local variable amountSavedSoFar and instead just typed Int, so the compiler thinks you're trying to pass in the type Int itself as the parameter, which is why you're getting that error.
Also, one unrelated thing, I find it interesting that if name is not able to be decoded, then you return nil, but then if savingsAmount, amountPerWeek, or amountSavedSoFar cannot be decoded then you've opted to crash the app by using the "crash here" operator, as!. You may also want to return nil if any of those can't be decoded either.
Ok... Umm I tried that, but when I do that it gives me more errors. 🙄: Expected expression in list of expressions Cannot invoke 'Savings.init' with an argument list of type '(name: String, photo: UIImage?, savingsAmount: String, amountPerWeek: String)'
– Evan
Nov 10 at 1:57
@Evan Ah, I failed to notice that you needed to usedecodeIntegerinstead ofdecodeObject. Glad you got it to work at least!
– TylerTheCompiler
Nov 10 at 4:34
add a comment |
up vote
0
down vote
You have this (I added line breaks to make it easier to read):
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: Int)
but I think you meant to type this:
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: amountSavedSoFar as! Int)
Basically, you forgot to pass in the local variable amountSavedSoFar and instead just typed Int, so the compiler thinks you're trying to pass in the type Int itself as the parameter, which is why you're getting that error.
Also, one unrelated thing, I find it interesting that if name is not able to be decoded, then you return nil, but then if savingsAmount, amountPerWeek, or amountSavedSoFar cannot be decoded then you've opted to crash the app by using the "crash here" operator, as!. You may also want to return nil if any of those can't be decoded either.
Ok... Umm I tried that, but when I do that it gives me more errors. 🙄: Expected expression in list of expressions Cannot invoke 'Savings.init' with an argument list of type '(name: String, photo: UIImage?, savingsAmount: String, amountPerWeek: String)'
– Evan
Nov 10 at 1:57
@Evan Ah, I failed to notice that you needed to usedecodeIntegerinstead ofdecodeObject. Glad you got it to work at least!
– TylerTheCompiler
Nov 10 at 4:34
add a comment |
up vote
0
down vote
up vote
0
down vote
You have this (I added line breaks to make it easier to read):
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: Int)
but I think you meant to type this:
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: amountSavedSoFar as! Int)
Basically, you forgot to pass in the local variable amountSavedSoFar and instead just typed Int, so the compiler thinks you're trying to pass in the type Int itself as the parameter, which is why you're getting that error.
Also, one unrelated thing, I find it interesting that if name is not able to be decoded, then you return nil, but then if savingsAmount, amountPerWeek, or amountSavedSoFar cannot be decoded then you've opted to crash the app by using the "crash here" operator, as!. You may also want to return nil if any of those can't be decoded either.
You have this (I added line breaks to make it easier to read):
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: Int)
but I think you meant to type this:
self.init(name: name,
photo: photo,
savingsAmount: savingsAmount as! String,
amountPerWeek: amountSavedPerWeek as! String,
amountSavedSoFar: amountSavedSoFar as! Int)
Basically, you forgot to pass in the local variable amountSavedSoFar and instead just typed Int, so the compiler thinks you're trying to pass in the type Int itself as the parameter, which is why you're getting that error.
Also, one unrelated thing, I find it interesting that if name is not able to be decoded, then you return nil, but then if savingsAmount, amountPerWeek, or amountSavedSoFar cannot be decoded then you've opted to crash the app by using the "crash here" operator, as!. You may also want to return nil if any of those can't be decoded either.
answered Nov 10 at 1:27
TylerTheCompiler
4,53522029
4,53522029
Ok... Umm I tried that, but when I do that it gives me more errors. 🙄: Expected expression in list of expressions Cannot invoke 'Savings.init' with an argument list of type '(name: String, photo: UIImage?, savingsAmount: String, amountPerWeek: String)'
– Evan
Nov 10 at 1:57
@Evan Ah, I failed to notice that you needed to usedecodeIntegerinstead ofdecodeObject. Glad you got it to work at least!
– TylerTheCompiler
Nov 10 at 4:34
add a comment |
Ok... Umm I tried that, but when I do that it gives me more errors. 🙄: Expected expression in list of expressions Cannot invoke 'Savings.init' with an argument list of type '(name: String, photo: UIImage?, savingsAmount: String, amountPerWeek: String)'
– Evan
Nov 10 at 1:57
@Evan Ah, I failed to notice that you needed to usedecodeIntegerinstead ofdecodeObject. Glad you got it to work at least!
– TylerTheCompiler
Nov 10 at 4:34
Ok... Umm I tried that, but when I do that it gives me more errors. 🙄: Expected expression in list of expressions Cannot invoke 'Savings.init' with an argument list of type '(name: String, photo: UIImage?, savingsAmount: String, amountPerWeek: String)'
– Evan
Nov 10 at 1:57
Ok... Umm I tried that, but when I do that it gives me more errors. 🙄: Expected expression in list of expressions Cannot invoke 'Savings.init' with an argument list of type '(name: String, photo: UIImage?, savingsAmount: String, amountPerWeek: String)'
– Evan
Nov 10 at 1:57
@Evan Ah, I failed to notice that you needed to use
decodeInteger instead of decodeObject. Glad you got it to work at least!– TylerTheCompiler
Nov 10 at 4:34
@Evan Ah, I failed to notice that you needed to use
decodeInteger instead of decodeObject. Glad you got it to work at least!– TylerTheCompiler
Nov 10 at 4:34
add a comment |
2
You need to pass an
Intvalue, not the keywordInt.– rmaddy
Nov 10 at 1:25
let amountSavedSoFar = aDecoder.decodeInteger(forKey: PropertyKey.amountSavedPerWeek)– Leo Dabus
Nov 10 at 1:56
self.init(name: name, photo: photo, savingsAmount: savingsAmount as! String, amountPerWeek: amountSavedPerWeek as! String, amountSavedSoFar: amountSavedSoFar)– Leo Dabus
Nov 10 at 1:56
1
@LeoDabus got it to work with that! Thanks!
– Evan
Nov 10 at 2:04