ios - why I have to unwrap value before I use -
a block defined below
// declare block ( optional ) typealias sorting = (([schedule], [string]) -> [schedule])? var sortschedule: sorting = { (schedules, sortdescription) in var array = [schedule]() string in sortdescription { (index, schedule) in schedules.enumerate() { if string == schedule.starttime { array.append(schedule) break } } } return array } at points, invoking block doing
let allschedules = sortschedule?(result, sortdescription()) schedule in allschedules // xcode complains @ here { .......... } im using ? because want make sure if block exists, something. however, xcode complains for loop
value of optional type [schedule]? not upwrapped, did mean use '!' or '?'? im not sure why because return type of block array can have 0 or more 1 items.
does know why xcode complaining.
you use ? in line let allschedules = sortschedule?(result, sortdescription()) not "for sure if block exists", note, understand can nil. behind scene allschedules have type array<schedule>?. , can not use for in cycle nil. better use optional binding:
if let allschedules = sortschedule?(result, sortdescription()) { schedule in allschedules { //.......... } }
Comments
Post a Comment