ios - how do you return nil if a certain type is passed to a function in Swift? -


ok trying return nil if type passed function. in case im passing in instance of class "blogpost" , type within blogpost. have array called "types" , have assigned variable videos last index of array. if type passed function return nil (so assuming im going need optional here returning possible nil) have far :-

enter image description here

so in need pass in instance of blog post return nil if type passed in. hope makes sense


update:

the types array defined follows:

let types : [string] = ["technology", "fashion", "animals"] 

this array referring in function. if last entry of array entered function need return nil

sure blogpost have empty string type

enter image description here

great im getting there ive done change blogpost.type choose 1 @ random. if specfic type chosen array how still getting error. have updated to

enter image description here

so need access 2 type in array , if access return nil. thoughts on that? drag on thanks

you passing blog blogpost.type parameter. not correct. should have either passed string parameter, or pass blogpost itself:

func randomviews(blog: blogpost) {     let videos = types[2]      if blog.type == videos {         // whatever want     }      // carry on } 

unrelated question @ hand, notice use let instead of var when defining videos. use let if value not (and cannot) change.

also note use lowercase letter v in videos, because cocoa naming conventions dictate variables start lowercase letters, whereas types, classes, structs, , enums start uppercase letters.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -