FLYNNLABAboutMeCodingActivityStudy 2024초등수학링크오프너릴리즈노트WordTest
swift Dictionary for in loop issue
2014-09-23
swift

ISSUE

swift 에서 Dictionary for in loop를 사용하다보면 순서를 보장해주지 않는다.
특히 순서가 중요할 경우 ㅠㅜ

ASIS

let views:[NSObject:AnyObject] = ["backgroundView":backgroundView, "photoGradient":photoGradient, "titleLabel":titleLabel, "fromTimeAgoLabel":fromTimeAgoLabel, "articleViewButton":articleViewButton, "closeButton":closeButton, "pagingLabel":pagingLabel]

for (viewName, uiview) in views as [String:UIView] {
    NSLog("\(viewName)")
    self.view.addSubview(uiview)
}

TOBE

순서가 보장되는 Array를 활용하자~

let views:[NSObject:AnyObject] = ["backgroundView":backgroundView, "photoGradient":photoGradient, "titleLabel":titleLabel, "fromTimeAgoLabel":fromTimeAgoLabel, "articleViewButton":articleViewButton, "closeButton":closeButton, "pagingLabel":pagingLabel]
let viewsForAddSubview:[AnyObject] = [backgroundView, photoGradient, titleLabel, ...]
for uiview in viewsForAddSubview as [UIView] {
    self.view.addSubview(uiview)
}