ios - swift valueForKeyPath doesn't work -


i have function

func findobjectinarray(array: [anyobject], field: string?, fieldvalue: anyobject, startfromthebeginning: bool, minindextemp: int, maxindextemp: int) -> anyobject? 

and there have:

var minindex = minindextemp         var maxindex = maxindextemp         if (array.count == 0 || minindex < 0 || maxindex < 0) {             return nil         }         if (minindex > maxindex) {             let rem = minindex;             minindex = maxindex;             maxindex = rem;         }         if (maxindex >= array.count) {             maxindex = array.count - 1;         }         let startindex = startfromthebeginning ? minindex : maxindex;         let endindex = startfromthebeginning ? maxindex : minindex;          (var = startindex; (startfromthebeginning ? <= endindex : >= 0); (startfromthebeginning ? i++ : i--)) {             let object = array[i]             var objectfieldvalue: anyobject? = nil             if (field != nil) {                 print("test = ", (object as! sad).adid)                 objectfieldvalue = object.valueforkeypath(field!)             } 

i logs before crash see test = 1 . means object converted sad , has field adid, on row objectfieldvalue = object.valueforkeypath(field!) application crashes error:

 *** terminating app due uncaught exception 'nsunknownkeyexception', reason: '[<project.sad 0x12d731930> valueforundefinedkey:]: class not key value coding-compliant key adid 

here class

class sad: nsobject {     var adid: int!     var bloggerid: int!     var createdon: nsdate!     var duration: int!     var fav: int!     var price: double!     var text: string!     var countries: [string]!     var categories: [string]!     var deleted: bool! } 

actually, can test same behavio in code simply:

create class:

class stest: nsobject {     var idtest: int?     var strtest: string? } 

and in appdelegate file:

func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool {     let testobj1 = stest()     testobj1.idtest = 1     testobj1.strtest = "test"     let objvalue = testobj1 .valueforkeypath("idtest")      return true } 

you see crash @ line let objvalue = testobj1 .valueforkeypath("idtest")

but if replace line let objvalue = testobj1 .valueforkeypath("strtest") work. problem in int value

you can't use kvc optional swift numeric structure types, such int, uint, float, double, , bool. put types not anyobject compatible, , can't bridged.

in example, use:

var adid: nsnumber! 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -