不应该使用关键字'可选'?

时间:2016-07-03 00:36:29

标签: swift optional unwrap

代码

func downloadimages (URL: NSURL) {
        let request = NSMutableURLRequest ( URL: URL)
        request.HTTPMethod = "GET"


    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error ) in

        guard error == nil else {

            print("we have an error from Server")
            return
        }
        var JSONData: AnyObject!
        do {

            JSONData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) /* as? [String:AnyObject?] */

        } catch {
            print (" We had a Parsing issue '\(data)'")
            return
        }
            print(JSONData)// Doesn't print 'Optional' word?????
            print(JSONData!)
        if let something = JSONData!["photos"]{

            print (something!)
            print(something) // This prints the word 'Optional as well'

        }

输出

//printed unwrapped--NOT GOOD! -- I didn't unwrap it with '!'
    {
        photos =     {
            page = 1;
            pages = 622374;
            perpage = 1;
            photo =         (
                            {
                    farm = 8;
                    id = 27765969370;
                    isfamily = 0;
                    isfriend = 0;
                    ispublic = 1;
                    owner = "8262787@N07";
                    secret = 6daeee7d68;
                    server = 7233;
                    title = "Stars, Planets and Lightning Bugs";
                }
            );
            total = 622374;
        };
        stat = ok;
    }
    // unwrapped printed--Good!
        {
            photos =     {
                page = 1;
                pages = 622374;
                perpage = 1;
                photo =         (
                                {
                        farm = 8;
                        id = 27765969370;
                        isfamily = 0;
                        isfriend = 0;
                        ispublic = 1;
                        owner = "8262787@N07";
                        secret = 6daeee7d68;
                        server = 7233;
                        title = "Stars, Planets and Lightning Bugs";
                    }
                );
                total = 622374;
            };
            stat = ok;
        }

   //Unwrapped printed--Good
    {
        page = 1;
        pages = 622374;
        perpage = 1;
        photo =     (
                    {
                farm = 8;
                id = 27765969370;
                isfamily = 0;
                isfriend = 0;
                ispublic = 1;
                owner = "8262787@N07";
                secret = 6daeee7d68;
                server = 7233;
                title = "Stars, Planets and Lightning Bugs";
            }
        );
        total = 622374;
    }

//wrapped and prints as optional--Good!
        Optional({
            page = 1;
            pages = 622374;
            perpage = 1;
            photo =     (
                        {
                    farm = 8;
                    id = 27765969370;
                    isfamily = 0;
                    isfriend = 0;
                    ispublic = 1;
                    owner = "8262787@N07";
                    secret = 6daeee7d68;
                    server = 7233;
                    title = "Stars, Planets and Lightning Bugs";
                }
            );
            total = 622374;
        })

我的困惑是,如果JSONData是可选的,那么为什么它在没有!的情况下打印为非可选项,如果它不是可选的,那么为什么它不会给出任何错误{ {1}}。打开非可选项是错误的!

我的猜测是我不明白我的Can not force unwrap value of non-optional type?类型是什么......

1 个答案:

答案 0 :(得分:2)

我的猜测是我不明白我的JSONData的类型是什么......

我相信这就是重点。 您已将JSONData声明为AnyObject!,也称为ImplicitlyUnwrappedOptional<AnyObject>

因此,在您的代码print(JSONData)中,隐式展开JSONData的值。

并检查Xcode的“快速帮助”窗格中something的类型。它应显示为AnyObject?,即Optional<AnyObject>

一个坏处是,当隐式展开发生时,它没有得到很好的记录。在确切知道何时发生这种情况之前,您可能需要了解有关ImplicitlyUnwrappedOptional的更多信息。