ios - Use result of an if statement as a variable -
i'm wondering if there's way of using returned value of method, checked if statement, variable if not nil.
i hope code example below helps explain i'm after.
let's have method returns uiimageview
, -(uiimageview *)imageviewexistsforid:(nsstring *)viewid
. in method somewhere else, want check if imageview returns value, , conditional, use if-statement, such:
if ([self imageviewexistsforid:<viewid>]) { // use returned imageview here } else { // 'nil' returned, no need use result }
normally, declare placeholder variable before if-statement, such as:
uiimageview *returnedimageview = [self imageviewexistsforid:<viewid>]; if (returnedimageview) { ...etc
i'm curious know if there way of passing result method, when first checked if-statement, without having store result in variable first, more akin to:
if ([self imageviewexistsforid:<viewid>]) { uiimageview *imageview = if-statement-result // ??? (i.e. don't call method again [self imageviewexistsforid:<viewid>]) }
disclaimer: asking purely intellectually curious angle, not 'issue' stopping me achieving in code. i'm keen learn more objective-c don't consider myself well-read enough in yet. can let me know particular point - apologies if i'm being incomprehensible!
no, there not way use result of if-condition. have declare placeholder variable.
it possible, , under circumstances permissible, initialize placeholder inside if
statement
uiimageview *returnedimageview; if ((returnedimageview = [self imageviewexistsforid:<viewid>])) {
note parentheses; using =
instead of ==
common mistake compiler may issue warning without wrapping assignment in parentheses.
Comments
Post a Comment