前提
Swift
cocoapodsでchartsの様なライブラリを入れたのですが、エラーが出ます。
NSNumberの部分で複数箇所エラーが出ている状況です。ライブラリのコードでどのように修正していいかわからなくて困っています。
ご教授いただけないでしょうか。よろしくお願いします。
発生している問題・エラーメッセージ
赤色警告 No exact matches in call to initializer 灰色警告 Candidate expects value of type 'CChar' (aka 'Int8') for parameter #1 (Foundation.NSNumber) Candidate expects value of type 'Double' for parameter #1 (Foundation.NSNumber) ・・・・
該当のソースコード
swift
1func textWithValue(formatter: FLFormatter) -> NSMutableAttributedString { 2 if let value = value { 3 let formattedValue = formatter.string(from: NSNumber(value: value)). //NSNumber エラー 4 let attributedText = NSMutableAttributedString(string: formattedValue, attributes: [.font : UIFont.boldSystemFont(ofSize: 13)]) 5 attributedText.append(NSAttributedString(string: " \(key)")) 6 return attributedText 7 } else { 8 return NSMutableAttributedString(string: key) 9 } 10 } 11 12 13var value: CGFloat? 14 15 public init(key: String, value: CGFloat? = nil, color: FLColor) { 16 self.key = key 17 self.value = value 18 self.color = color 19 } 20
何というライブラリでしょうか?
FLChartsです!
https://github.com/francescoleoni98/FLCharts
NSNumber(value: value) は、value が CGFloat なら問題ないはずですが、
FLFormatter の string(from:) メソッドが public じゃないので、ライブラリの外からは使えませんね…。
https://github.com/francescoleoni98/FLCharts/blob/main/Sources/FLCharts/Helpers/Formatters.swift#L57
hoshi-takanori様 お時間割いていただきありがとうございます。
let formattedValue = formatter.string(from: NSNumber(value: Float(value))にしたらエラー消えたのですがこの形は適切でしょうか?
また、publicメソッドがあまりよく分かっていないのですが、単純にpublic加えるだけでいいでしょうか?
public func string(from value: NSNumber) -> String {
return formatter.string(for: value) ?? "N/D"
}
回答1件
あなたの回答
tips
プレビュー