NSDictionary - 无法调用" init"

时间:2015-05-03 19:22:57

标签: swift

这是代码

let dictionary = NSDictionary(objects: [user.username, image], forKeys: ["username", "image"])

我收到错误

无法调用" init"带参数列表类型(对象:$ T3,forKeys:$ T7)

显示如何创建user.username和image的更多代码

let users = objects

                        for userId in self.arrayUserIds
                        {
                            let user = self.getUserFromUserId(userId as String, arrayUsers: users)

                            self.arrayUserFriends.addObject(user)

                            var query = PFQuery(className: "UserPhoto")
                            query.whereKey("user", equalTo: user)

                            query.findObjectsInBackgroundWithBlock{
                                (NSArray objects, NSError error) -> Void in

                                if(objects.count != 0)
                                {
                                    let object = objects[objects.count - 1] as PFObject
                                    let theImage = object["imageData"] as PFFile

                                    let imageData:NSData    = theImage.getData()
                                    let image               = UIImage(data: imageData)


                                    let dictionary = NSDictionary(objects: [user.username, image], forKeys: ["username", "image"])
                                    self.arrayFriends.addObject(dictionary)

                                    self.chatTable.reloadData()
                                }
                            }
                        }

1 个答案:

答案 0 :(得分:0)

您正在调用的UIImage初始化程序返回一个Optional,

public class BlackJack {

    static String[] deck = new String[52];

    //Creates Deck of cards
    public static void main (String[] args)  {      
        String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
        String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                "Jack", "Queen", "King", "Ace" };

        for (int a=0; a < rank.length; a++) {
            for (int b=0; b < suit.length; b++){
                deck[suit.length*a + b] = (rank[a] +" of "+ suit[b]);
            }
        }

        //Trying to get this to print out the returned card name
        String card = "G";
        randomCard(card);
        System.out.println(card);
    }

    //Pulls a random card from the deck
    public static String randomCard(String cardName) {          
        //Generates a random number between 0 and 52
        int card = (int)(Math.random()*53);
        cardName = (deck[card]);
        return cardName;

    }

}

来自文档: 返回值 初始化的UIImage对象,如果方法无法从指定的数据初始化图像,则为nil。

您需要强行打开可选内容(请注意图像后面的标记):

init?(data data: NSData)