Saturday 15 June 2013

ios - KVO Binding failing in swift -



ios - KVO Binding failing in swift -

this bug has me frustrated. using kvo follows:

dynamic var product: nsnumber? { { homecoming (defaults.objectforkey("product") != nil ? defaults.integerforkey("product") : nil) } set(newvalue) { if newvalue != product { willchangevalueforkey("product") if allow value = newvalue { defaults.setinteger(value.integervalue, forkey: "product") } else { defaults.removeobjectforkey("product") } didchangevalueforkey("product") reader.product = newvalue?.integervalue refresh() } } } override class func automaticallynotifiesobserversforkey(key: string) -> bool { homecoming true }

another object modifies property follows;

info.product = nsnumber(short: prodid)

when called, result next error;

thread 1: exc_bad_access(code=1, address = 0x0)

and stack trace:

#0 0x37c4cf66 in objc_msgsend () #1 0x2acca772 in nskeyvaluepushpendingnotificationperthread () #2 0x2acbae3c in nskeyvaluewillchange () #3 0x2aca7c04 in -[nsobject(nskeyvalueobservernotification) willchangevalueforkey:] () #4 0x2acdafc0 in _nssetobjectvalueandnotify () #5 0x002a3c44 in module.myview.pickerview (module.myview)(objectivec.uipickerview, didselectrow : swift.int, incomponent : swift.int) -> () @ /<***>/myview.swift:122

now, i've set breakpoints , prints , determined nskeyvaluewillchange called before var setter code called, appears not beingness used in same way in objective c, message nskeyvaluewillchange beingness sent before the setter beingness called. in objective c setter had manually phone call willchangevalueforkey("product"). have thought happening?

on side note, next s code crash occurs. error occurs on 3rd line, guessing r9 beingness 0x0 has it.

libobjc.a.dylib`objc_msgsend: 0x37c4cf60: cbz r0, 0x37c4cf9e ; objc_msgsend + 62 0x37c4cf62: ldr.w r9, [r0] 0x37c4cf66: ldrh.w r12, [r9, #12] 0x37c4cf6a: ldr.w r9, [r9, #8] 0x37c4cf6e: and.w r12, r12, r1 0x37c4cf72: add.w r9, r9, r12, lsl #3 0x37c4cf76: ldr.w r12, [r9] 0x37c4cf7a: teq.w r12, r1 0x37c4cf7e: bne 0x37c4cf86 ; objc_msgsend + 38 0x37c4cf80: ldr.w r12, [r9, #4] 0x37c4cf84: bx r12 0x37c4cf86: cmp.w r12, #0x1 0x37c4cf8a: blo 0x37c4cf98 ; objc_msgsend + 56 0x37c4cf8c: eq 0x37c4cf8e: ldreq.w r9, [r9, #4] 0x37c4cf92: ldr r12, [r9, #8]! 0x37c4cf96: b 0x37c4cf7a ; objc_msgsend + 26 0x37c4cf98: ldr.w r9, [r0] 0x37c4cf9c: b 0x37c4d1e0 ; _objc_msgsend_uncached 0x37c4cf9e: mov.w r1, #0x0 0x37c4cfa2: bx lr

you have create sure phone call didchangevalueforkey: , willchangevalueforkey:. dont need override getter , setter trigger kvo notification. dont know extact implementation looking @ class, if info class had trigger notification on product change, this,

class myinfoclass { var product: nsnumber?{ willset{ self.willchangevalueforkey("product") } didset{ self.didchangevalueforkey("product") } } override class func automaticallynotifiesobserversforkey(key: string) -> bool { if key == "product"{ homecoming true }else{ homecoming automaticallynotifiesobserversforkey(key) } } }

but if wish utilize dynamic, dont need override kvo method automaticallynotifiesobserversforkey: , not need trigger willchangevalueforkey: , didchangevalueforkey:. please mind class should subclass of nsobject. in case class this,

class myinfoclass:nsobject{ dynamic var product: nsnumber? }

ios swift exc-bad-access key-value-observing

No comments:

Post a Comment