-1

代码

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 是可选的,那么为什么它打印为非可选没有!,如果它不是可选的,那么它为什么不给出任何错误Can not force unwrap value of non-optional type?。由于展开非可选是错误的!

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

4

1 回答 1

1

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

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

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

something并在 Xcode 的快速帮助窗格中检查类型。它应该显示为AnyObject?aka Optional<AnyObject>

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

于 2016-07-03T00:53:42.777 回答