2017-09-20 1 views
4

방금 ​​Xcode 9로 업데이트하고 신속한 3에서 신속한 4로 내 앱을 변환했습니다. 문자열을 사용하여 축과 다른 변수에 레이블을 지정하는 그래프가 있습니다. 그래서 moneyAxisString = "Money"가 있습니다.스위프트 4 '[String : AnyObject]?'유형의 값을 변환 할 수 없습니다. 예상 인수 형식 '[NSAttributedStringKey : Any]?'

:

attributes = [ 
     NSAttributedStringKey.foregroundColor: fieldColor, 
     NSAttributedStringKey.font: fieldFont!, 
     NSAttributedStringKey.paragraphStyle: style 

    ] 

이제 내 응용 프로그램이 컴파일되지 않습니다 내가 메시지를 얻고을 다음과 같이

속성
moneyAxisString.draw(in: CGRect(x: CGFloat(coordinateXOriginLT + axisLength/3), y: CGFloat(coordinateYOriginRT + axisLength + 5 * unitDim), width: CGFloat(300 * unitDim), height: CGFloat(100 * unitDim)), withAttributes: attributes as? [String : AnyObject]) 

정의 사전입니다 : 는 이전에이 코드를 사용하여 그릴 수 있었다

'[String : AnyObject]?'유형의 값을 변환 할 수 없습니다. 예상 인수 형식 '[NSAttributedStringKey : Any]?'

답변

6

그것은 유형 불일치가있다 : [String : AnyObject] 명확하지 [NSAttributedStringKey : Any]

인 선언을 볼 수 NSAttributedStringKey을 ⌥ 클릭합니다.


이 솔루션은 다운

..., withAttributes: attributes) 

캐스팅 제거하고 간단하게

attributes = [.foregroundColor: fieldColor, 
       .font: fieldFont!, 
       .paragraphStyle: style] 
+0

다운 캐스트를 제거 할 때 : 'NSDictionary'유형의 값을 예상 인수 유형 '[NSAttributedStringKey : Any]'으로 변환 할 수 없습니다. 그리고 그것은 다음과 같이 말합니다 : Insert as '! [NSAttributedStringKey : Any] '. 또한 속성에 대한 코드를 줄이면 "표현식이 사용되지 않는 l- 값으로 해석됩니다"및 "예상 표현식"및 "행의 연속 문장은 ';'으로 구분해야합니다."; "삽입하십시오. 관련성이 있습니다 처음에 속성을 다음과 같이 정의했습니다 : var attributes : NSDictionary = [:] –

+0

'NSDictionary' 대신'var attributes = [NSAttributedStringKey : Any]()'를 선언하면, 주석을 제거 할 수도 있습니다. 어쨌든. – vadian

0

이 시도 작성 attributes

var attributes = [NSAttributedStringKey : Any]() 

로 선언하는 것입니다 :

class func getCustomStringStyle() -> [NSAttributedStringKey: Any] 
    { 
     return [ 
      NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 16), // or your fieldFont 
      NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.black, // or your fieldColor 
      NSAttributedStringKey(rawValue: NSAttributedStringKey.paragraphStyle.rawValue): NSParagraphStyle.default // or your style 
     ] 
    } 

나 :

class func getCustomStringStyle() -> [String: Any] 
    { 
     return [ 
      NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 16), 
      NSAttributedStringKey.foregroundColor.rawValue: UIColor.black, 
      NSAttributedStringKey.paragraphStyle.rawValue:NSParagraphStyle.default 
     ] 
    } 
0

NSAttributedStringKey 그러나, 다른 객체가NSAttributedStringKey 분명히 같은 시간에 업데이트되지 않은 사용 그 스위프트 4에 구조체로 변경되었습니다.

let attributes = [ 
    NSAttributedStringKey.font.rawValue: UIFont(name: "Helvetica-Bold", size: 15.0)!, 
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.white 
] as [String : Any] 
: String의에 키 이름을 돌려 - 다른 코드
을 변경하지 않고

가장 쉬운 수정, NSAttributedStringKey 세터의.rawValue모든 귀하의 항목을 추가하는 것입니다

as!이 필요하지 않습니다.

다른 방법으로는 선행 [String : Any]로 배열을 선언함으로써 마지막에 as 캐스트를 건너 뛸 수 있습니다 : 물론

let attributes: [String : Any] = [ 
    NSAttributedStringKey.font.rawValue: UIFont(name: "Helvetica-Bold", size: 15.0)!, 
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.white 
] 

, 당신은 아직도 당신이 설정 한 각 NSAttributedStringKey 항목에 대한 .rawValue를 추가해야합니다.

관련 문제