swift - Loop through [AnyObject]? results in does not have a member named generator -
why can't loop through [anyobject]?
directly? mean there no named generator? proper technique looping through [anyobject]?
type?
this code giving me error telling me not have fellow member named generator.
for screen in nsscreen.screens() { var result : bool = workspace.setdesktopimageurl(imgurl, forscreen: screen, options: nil, error: &error) }
screens
returns optional, before using actual value have disclose - recommended method optional binding:
if allow screens = nsscreen.screens() { screen in screens { var result : bool = workspace.setdesktopimageurl(imgurl, forscreen: screen, options: nil, error: &error) } }
read more optionals
note nsscreen.screens
returns [anyobject]?
, might want cast array [nsscreen]
in optional binding:
if allow screens = nsscreen.screens() as? [nsscreen] { screen in screens { var result : bool = workspace.setdesktopimageurl(imgurl, forscreen: screen, options: nil, error: &error) } }
addendum reply question in comment: why error message says [anyobject]? not have fellow member named generator
an optional of different type value contains (an optional enum). can iterate array, cannot iterate on integer or enum.
to understand difference, allow me create real life example: purchase new tv on ebay, bundle shipped you, first thing check if bundle (the optional) empty (nil). 1 time verify tv inside, have disclose it, , set box aside. cannot utilize tv while it's in package. similarly, optional container: not value contains, , doesn't have same type. can empty, or can contain valid value.
swift
No comments:
Post a Comment