ios - Swift, check if array has value at index -
var cellheights: [cgfloat] = [cgfloat]() if allow height = self.cellheights[index] as? cgfloat { self.cellheights[index] = cell.frame.size.height } else { self.cellheights.append(cell.frame.size.height) }
i need check whether element @ specified index exists. above code not work, build error:
conditional downcast cgfloat cgfloat succeeds
i tried with:
if allow height = self.cellheights[index] {} but failed:
bound value in conditional binding must of optional type any ideas whats wrong?
cellheights array containing non-optional cgfloat. of elements cannot nil, such if index exists, element on index cgfloat.
what trying possible if create array of optionals:
var cellheights: [cgfloat?] = [cgfloat?]() and in case optional binding should used follows:
if allow height = cellheights[index] { cellheights[index] = cell.frame.size.height } else { cellheights.append(cell.frame.size.height) } i suggest read 1 time again optionals
ios swift nsarray
No comments:
Post a Comment