ios - Sending an NSTimer target message to a different class; extracting its userInfo parameter -
i using nstimer send message on interval. here code :
{ // .... var params : [string] = [] params.append(conversion) params.append(message) let timer = nstimer(firedate: date, interval: 60, target: self, selector: selector("importtextmessage.sendmessage:"), userinfo: params, repeats: true) nsrunloop.mainrunloop().addtimer(timer, formode: nsrunloopcommonmodes) // ... } func sendmessage(params: [string]){ ...}
i have tried changing swift 2.2 syntax:
let timer = nstimer(firedate: date, interval: 60, target: self, selector: #selector(importtextmessage.sendmessage(_:)), userinfo: params, repeats: true)
but not change anything.
from every other question posted "unrecognized selector sent instance", response "include colon in selector knows grab arguments userinfo", have included that, , can't figure out wrong.
what note:
the parameters function , parameters passed in through nstimer userinfo match up. both arrays of strings.
if means anything, code failing while sendmessage being called. not make sendmessage.
i getting odd warning saying "string literal not valid objective-c selector"
i tried changing code sendmessage take timer argument 1 user suggested : func sendmessage(timer: nstimer){
but still gives same error.
thank in advance, appreciate it.
edit: here function runs timer: //save of data @ibaction func savetext(sender: anyobject) { var phone : double var active : int var frequency : double var message : string var date : nsdate
phone = double(currentnumber)! active = 1 message = mytextview.text date = mydatepicker.date switch self.frequency.selectedrowincomponent(0) { case 0: frequency = 1 case 1: frequency = 3 case 2: frequency = 6 case 3: frequency = 24 case 4: frequency = 168 case 5: frequency = 744 default: frequency = 8760 } importtextmessage.seedmessage(phone, active: active, frequency: frequency, message: message, date: date) print(date) let conversion : string = "+1" + string(int(phone)) //importtextmessage.sendmessage("ryan", to: conversion, message: message) var params : [string] = [] params.append(conversion) params.append(message) //importtextmessage.sendmessage(params) let timer = nstimer(firedate: date, interval: 60, target: self, selector: #selector(importtextmessage.sendmessage(_:)), userinfo: params, repeats: true) // nsrunloop.mainrunloop().addtimer(timer, formode: nsrunloopcommonmodes) }
edit: exact error mssg:
unrecognized selector sent instance 0x7ffe6b915460 2016-07-25 13:26:09.092 harass kate[53524:9000621] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[harass_your_kate.addmessageviewcontroller sendmessage:]: unrecognized selector sent instance 0x7ffe6b915460
the api of nstimer such receiver (target function) must take 1 , 1 parameter, of type nstimer.
the message send target when timer fires.
the selector should have following signature: timerfiremethod: (including colon indicate method takes argument). timer passes argument, method adopt following pattern:
- (void)timerfiremethod:(nstimer *)timer
in swift signature must be
edit: updated match code exactly
note: swift 2.2 syntax
class textmessageviewcontroller: uiviewcontroller { var importtextmessage: addmessageviewcontroller // init'd somehow override func viewdidload() { super.viewdidload() // ... // note how selector matches *the name of class* // understand now, sendmessage method // declared in addmessageviewcontroller class, // , importtextmessage name of instance of class. // , we're in different class now, textmessageviewcontroller, // using `self` not make sense here. let timer = nstimer(firedate: date, interval: 60, target: importtextmessage, selector: #selector(addmessageviewcontroller.sendmessage(_:)), userinfo: params, repeats: true) nsrunloop.mainrunloop().addtimer(timer, formode: nsrunloopcommonmodes) } } class addmessageviewcontroller: uiviewcontroller { // func must not private func sendmessage(timer: nstimer) { // , example print("params: \(timer.userinfo)") } }
as far userinfo dictionary, property of timer object, not parameter target function itself.
the second mistake selector syntax failing use name of class declared, not name of instance variable has reference class instance. include more context in question it'll solved faster!
the third mistake you're using wrong class name. if use self
, class name in selector must match name of current class. since want message to go different class, must use that reference instead. see updated code.
i think still don't grasp object references, , types, , how must match, , how self
reference contextual object.
Comments
Post a Comment