about NSDictionary question


(张辉) #1

HI everybody:

I have a question about NSDictionary.
I defined a instance of NSDictionary, when I set value to this instance , it throw a exception.

code:

let userDict = NSDictionary()

userDict.setValue(self.userName.text!, forUndefinedKey: "userName2")

exception:

2016-02-24 00:50:26.931 TravelAssistant[1900:792534] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x7fc430404810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key userName2.'

How to fix this bug?

thx.


(David Smith) #2

Hi,

Try using setObject(forKey:) rather than setValue(forUndefinedKey:).

  David

···

On Feb 24, 2016, at 1:12 AM, 张辉 via swift-dev <swift-dev@swift.org> wrote:

HI everybody:

I have a question about NSDictionary.
I defined a instance of NSDictionary, when I set value to this instance , it throw a exception.

code:
let userDict = NSDictionary()
userDict.setValue(self.userName.text!, forUndefinedKey: "userName2")

exception:
2016-02-24 00:50:26.931 TravelAssistant[1900:792534] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x7fc430404810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key userName2.'

How to fix this bug?

thx.
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev


(张辉) #3

No such method

I use swift2.1

···

At 2016-02-24 17:58:20, "David Smith" <david_smith@apple.com> wrote:
Hi,

Try using setObject(forKey:) rather than setValue(forUndefinedKey:).

David

On Feb 24, 2016, at 1:12 AM, 张辉 via swift-dev <swift-dev@swift.org> wrote:

HI everybody:

I have a question about NSDictionary.
I defined a instance of NSDictionary, when I set value to this instance , it throw a exception.

code:
let userDict = NSDictionary()
userDict.setValue(self.userName.text!, forUndefinedKey: "userName2")

exception:
2016-02-24 00:50:26.931 TravelAssistant[1900:792534] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x7fc430404810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key userName2.'

How to fix this bug?

thx.
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev


(张辉) #4

I change NSDictionary to NSMutableDictionary

it's resolved

thank you

···

At 2016-02-24 18:06:56, "张辉" <tianya_z@163.com> wrote:

No such method

I use swift2.1

At 2016-02-24 17:58:20, "David Smith" <david_smith@apple.com> wrote:
Hi,

Try using setObject(forKey:) rather than setValue(forUndefinedKey:).

David

On Feb 24, 2016, at 1:12 AM, 张辉 via swift-dev <swift-dev@swift.org> wrote:

HI everybody:

I have a question about NSDictionary.
I defined a instance of NSDictionary, when I set value to this instance , it throw a exception.

code:
let userDict = NSDictionary()
userDict.setValue(self.userName.text!, forUndefinedKey: "userName2")

exception:
2016-02-24 00:50:26.931 TravelAssistant[1900:792534] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x7fc430404810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key userName2.'

How to fix this bug?

thx.
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev


(张辉) #5

I change NSDictionary to NSMutableDictionary

it's resolved

thank you

···

At 2016-02-24 18:06:56, "张辉" <tianya_z@163.com> wrote:

No such method

I use swift2.1

At 2016-02-24 17:58:20, "David Smith" <david_smith@apple.com> wrote:
Hi,

Try using setObject(forKey:) rather than setValue(forUndefinedKey:).

David

On Feb 24, 2016, at 1:12 AM, 张辉 via swift-dev <swift-dev@swift.org> wrote:

HI everybody:

I have a question about NSDictionary.
I defined a instance of NSDictionary, when I set value to this instance , it throw a exception.

code:
let userDict = NSDictionary()
userDict.setValue(self.userName.text!, forUndefinedKey: "userName2")

exception:
2016-02-24 00:50:26.931 TravelAssistant[1900:792534] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x7fc430404810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key userName2.'

How to fix this bug?

thx.
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev