Swift 4条件编译逻辑无法正常工作

时间:2017-08-23 03:55:56

标签: compilation syntax-error conditional swift4

这里有一些简单的代码来说明我遇到的问题:

//first, create an upload session
var httpResponse = await _httpClient.StartAuthenticatedRequest(url, HttpMethod.Post).SendAsync(ct);
if (httpResponse.StatusCode != HttpStatusCode.OK)
{
    return new HttpResult<IRemoteItemHandle>(httpResponse, null);
}


//get the upload URL
var uploadSessionRequestObject = await HttpClientHelper.ReadResponseAsJObjectAsync(httpResponse);

var uploadUrl = (string)uploadSessionRequestObject["uploadUrl"];
if (uploadUrl == null)
{
    Debug.WriteLine("Successful OneDrive CreateSession request had invalid body!");
    //TODO: what to do here?
}

//the length of the file total
var length = data.Length;

//setup the headers
var headers = new List<KeyValuePair<string, string>>()
{
    new KeyValuePair<string, string>("Content-Length", ""),
    new KeyValuePair<string, string>("Content-Range","")
};

JObject responseJObject;
//the response that will be returned
HttpResponseMessage response = null;

//get the chunks
List<Tuple<long, long>> chunks;
do
{

    HttpResult<List<Tuple<long, long>>> chunksResult;
    //get the chunks
    do
    {
        chunksResult = await RetrieveLargeUploadChunksAsync(uploadUrl, _10MB, length, ct);
        //TODO: should we delay on failure?
    } while (chunksResult.Value == null);//keep trying to get thre results until we're successful

    chunks = chunksResult.Value;

    //upload each fragment
    var chunkStream = new ChunkedReadStreamWrapper(data);
    foreach (var fragment in chunks)
    {
        //setup the chunked stream with the next fragment
        chunkStream.ChunkStart = fragment.Item1;

        //the size is one more than the difference (because the range is inclusive)
        chunkStream.ChunkSize = fragment.Item2 - fragment.Item1 + 1;

        //setup the headers for this request
        headers[0] = new KeyValuePair<string, string>("Content-Length", chunkStream.ChunkSize.ToString());
        headers[1] = new KeyValuePair<string, string>("Content-Range", $"bytes {fragment.Item1}-{fragment.Item2}/{length}");

        //submit the request until it is successful
        do
        {
            //this should not be authenticated
            response = await _httpClient.StartRequest(uploadUrl, HttpMethod.Put)
                .SetContent(chunkStream)
                .SetContentHeaders(headers)
                .SendAsync(ct);

        } while (!response.IsSuccessStatusCode); // keep retrying until success
    }

    //parse the response to see if there are more chunks or the final metadata
    responseJObject = await HttpClientHelper.ReadResponseAsJObjectAsync(response);

    //try to get chunks from the response to see if we need to retry anything
    chunks = ParseLargeUploadChunks(responseJObject, _10MB, length);
}
while (chunks.Count > 0);//keep going until no chunks left

无论TEST是否真实&#39;或者&#39; false&#39;,&#39; #if!TEST&#39;总是执行。 &#39; #if TEST&#39;分支永远不会被执行。

这发生在Playground和编译代码中。 Xcode 9 Beta 5

1 个答案:

答案 0 :(得分:0)

您在TEST局部变量和TEST编译器标志之间感到困惑。

  • 您拥有的是名为TEST
  • 的本地变量
  • 您正在测试的是存在名为TEST
  • 的编译器标志

要设置编译器标志,请转到目标的构建设置并搜索Other Swift Flags,然后将-DTEST添加到Debug构建中。

此外,与C编译器标志相比,Swift编译器标志更简单:标志是否存在。您无法将int值或类似值关联起来。因此,获得其价值毫无意义。这是一个真/假的情况:你知道它存在与否;没有与之相关的其他价值。