在没有更多上下文的情况下获取表达式类型不明确

时间:2016-04-21 06:55:54

标签: ios swift swift2 health-kit

我正在尝试请求授权使用Health应用数据。

我收到以下错误:

FirstViewController.swift:44:9: Expression type '(typesToShare: _, readTypes: Set<HKSampleType>?, completion: (_, _) -> Void)' (aka '(typesToShare: _, readTypes: Optional<Set<HKSampleType>>, completion: (_, _) -> ())') is ambiguous without more context

在44:9,其中typesToShare:nil

let healthStore: HKHealthStore? = {
        if HKHealthStore.isHealthDataAvailable() {
            return HKHealthStore()
        } else {
            return nil
        }
    }()

    let dateOfBirthCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierDateOfBirth)

    let biologicalSexCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierBiologicalSex)

    let bloodTypeCharacteristic = HKCharacteristicType.characteristicTypeForIdentifier(
        HKCharacteristicTypeIdentifierBloodType)

    let dataTypesToRead: Set<HKSampleType>? = NSSet(objects:
        dateOfBirthCharacteristic!, biologicalSexCharacteristic!, bloodTypeCharacteristic!) as? Set<HKSampleType>

    let dataTypesToShare: Set<HKSampleType>? = NSSet() as? Set<HKSampleType>

    // Making the request
    let healthData = healthStore?.requestAuthorizationToShareTypes
    (typesToShare: nil,
     readTypes: dataTypesToRead,
     completion: { (success, error) -> Void in
                                            if success {
                                                println("success")
                                            } else {
                                                println(error.description)
                                            }
    })

我是iOS新手,之前没有使用过HealthKit。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我也是healthKit的新手,但尝试在typesToShare参数的nil处使用dataTypesToShare变量。更好的是使用与dataTypesToRead相同的方式创建dataTypesToShare。并且此函数不返回任何内容。您可以尝试使用闭包来获得完成块成功 。

   var HttpsAgent = require('agentkeepalive').HttpsAgent;

   var keepaliveAgent = new HttpsAgent({
        maxSockets: 100,
        maxFreeSockets: 10,
        timeout: 60000,
        keepAliveTimeout: 30000 // free socket keepalive for 30 seconds 
   });

   var options = {
        host: WECHAT_HOST,
        path: WECHAT_PREPAY_URL,
        method: "POST",
        headers: {
            "Content-Type": "application/xml",
            "Content-Length": requestXml.length
        },
        agent: keepaliveAgent
    };
    var request = require("https").request(options, function (res) {

        res.on("data", function (chunk) {
            ....
        });

        res.on("end", function () {
            ...
        });
    });

    request.on("error", function (err) {
        ...
    });
    //Logger.Write("Request ready");
    //console.log(requestXml.toString("utf-8"));
    request.write(requestXml);
    //Logger.Write("Data sent");
    request.end();//The problem is here, wait for about 12 seconds, if I invoke same request in firefox, the remote server response immediately.