简体   繁体   English

在 SWIFT 字符串插值中没有更多上下文的表达式类型不明确

[英]Type of expression is ambiguous without more context in SWIFT string interpolation

I have React Native project.我有 React Native 项目。 And I need in the native part of IOS to make string interpolation of formattedValue with a character, but I get the error "Type of expression is ambiguous without more context", and if I try the same code in the playground everything is working with out error.而且我需要在 IOS 的本机部分使用字符对 formattedValue 进行字符串插值,但是我收到错误“没有更多上下文的表达式类型不明确”,如果我在操场上尝试相同的代码,一切都将正常工作错误。 I'm not so experienced in SWIFT, can you tell me please why I get this error?我在 SWIFT 方面经验不足,你能告诉我为什么我会收到这个错误吗? And which the best way to make a string interpolation?进行字符串插值的最佳方法是什么?

import Foundation
import SciChart.Protected.SCILabelProviderBase

class SCIAxisNumericLabelProvider: SCILabelProviderBase<ISCINumericAxis> {
    var format: String?
    var specialChar: String?

    init(format: String?) {
        let pattern = "[^0-9.]"
        let regex = try! NSRegularExpression(pattern: pattern)
        let formatValue = regex.stringByReplacingMatches(in: format ?? "", range: NSMakeRange(0, format?.count ?? 0), withTemplate: "")

        self.format = formatValue
        self.specialChar = format?.replacingOccurrences(of: "\(formatValue)", with: "")
        
        super.init(axisType: ISCINumericAxis.self)
    }

    override func formatLabel(_ dataValue: ISCIComparable!) -> ISCIString! {
        let dataValueToDouble = dataValue.toDouble()
        let formattedValue = NSString(format: NSString(string: self.toFormat()), dataValueToDouble == -0 ? 0 : dataValueToDouble)
        
        if let char = specialChar {
            return "\(formattedValue) \(NSString(char))" // Here I get the error Type of expression is ambiguous without more context
        }
        
        return formattedValue
    }

    override func formatCursorLabel(_ dataValue: ISCIComparable!) -> ISCIString! {
      return formatLabel(dataValue)
    }

    func toFormat() -> String {
        if (self.format != nil) {
            let a = self.format!.split(separator: ".")
             if (a.count > 1) {
                return "%0." + String(a[1].count) + "f"
             }
         }
        return "%0.f"
    }
    
    func extractSpecialChar(value: String) {
        
    }
}

In the end of the day I decided this error, I separated string interpolation and converting:最终我决定了这个错误,我将字符串插值和转换分开:

    override func formatLabel(_ dataValue: ISCIComparable!) -> ISCIString! {
        let formatterValue = self.toFormat();
        let valueDouble = dataValue.toDouble()
        let valueDoubleExcludedNegZero = valueDouble == -0 ? 0 : valueDouble
        let precisionString = String(format: formatterValue, valueDoubleExcludedNegZero)

        if let char = specialChar {
            let resultString = precisionString + " " + char
            return NSString(string: resultString)
        }

        return NSString(string: precisionString)
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 2:表达式在没有更多上下文的情况下是模糊的 - Swift 2: Type of expression is ambiguous without more context 在Swift 3中没有更多上下文的表达式是模糊的 - Type of expression is ambiguous without more context in Swift 3 迅速地,“表达的类型是模棱两可的,没有更多的上下文”? - In swift,“Type of expression is ambiguous without more context”? Swift 3中的“表达类型不明确,没有更多上下文” - “Type of expression is ambiguous without more context” in Swift 3 如何解决“在没有更多上下文的情况下含糊不清的表达类型”? 在迅速3.0 - how to resolve “Type of expression in ambiguous without more context”? in swift 3.0 Swift和HealthKit:在没有更多上下文的情况下,表达类型过于含糊 - Swift and HealthKit: type of expression too ambiguous without more context Swift 3:表达式类型[UIImage]在没有更多上下文的情况下是模棱两可的 - Swift 3: Expression type [UIImage] is ambiguous without more context Swift:“表达式类型在没有更多上下文的情况下不明确”与 URLSession - Swift: “Type of expression is ambiguous without more context” with URLSession 表达式类型不明确,没有更多上下文Healthkit iOS Swift - Expression Type ambiguous without more context Healthkit ios Swift 斯威夫特2:对于字典来说,表达式类型不明确,没有更多上下文 - Swift 2: Type of expression is ambiguous without more context, for a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM