ios - 如何处理空 PFFile 对象(解析后端,Swift)

标签 ios swift parse-platform

我在 Parse(后端)上存储了一个用户列表,其中有一个名为“profile_picture”的列,用于存储用户的个人资料图片。但是,有些用户没有个人资料图片,因此他们的 profile_picture 列是“未定义”(没有值)。

我正在使用搜索栏查询所有用户,并使用用户的个人资料图片和用户名更新表格 View 。我通过将用户名附加到 var searchResults = String ,并将个人资料图片附加到查询后的 var searchImages = PFFile 来做到这一点,如下所示:

            let query = PFQuery.orQuery(withSubqueries: [usernameQuery!,fbUsername!, firstnameQuery!, lastnameQuery!]);

        searchActive = true;

        query.findObjectsInBackground { (objects, error) in

            if error != nil {

                print("There was an error getting userlist");

            }

            else {

                if let users = objects {

                    self.searchResults.removeAll(keepingCapacity: false);

                    for object in users {

                        if let user = object.object(forKey: "username") as? String {

                            self.searchResults.append(user);
                        }

                        if let picture = object.object(forKey: "profile_picture") as? PFFile {

                            self.searchImages.append(picture);

                        }

                        self.searchTableView.reloadData();

                    }

问题在于,当“profile_picture”列为空时,它不会附加任何内容,这意味着 searchResults 数组(包含用户名)和 searchImages 数组(包含 PFFiles)具有不同的大小,这会导致不协调值的分配。它们应该是并行阵列。我使用这些数组中的值来分配表格单元格中的值。

希望大家能理解我的问题!提前致谢

最佳答案

所以您的用户名字段绝对不为空。我认为你可以在检查 profile_picture 是否为 nil 后添加一个 else 。喜欢:

if let picture = object.object(forKey: "profile_picture") as? PFFile {
    self.searchImages.append(picture);
} else {
    self.searchImages.append(UIImage(named:"placeholder"))
}

关于ios - 如何处理空 PFFile 对象(解析后端,Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165482/

相关文章:

swift - 检测 UIViewController 何时被 tvOS 中的 Remote 菜单按钮关闭

linux - Swift 字符串 hasSuffix 错误

ios - 将 UIView 添加到 UICollectionViewController 的底部

swift - 解析视频上传 - fatal error : unexpectedly found nil while unwrapping an Optional Value

parse-platform - 正在进行的 parse.com 请求是否继续计入 API 限制?

ios - 在 Xcode 10 中使用启动图像时,iPhone XS Max/XR 无法使用原始分辨率

objective-c - 如何清除绘制 uiview?

c++ - 连接 2 个 char* 会产生奇怪的结果

ios - Swift 中下一个可用日期的日期格式

ios - ParseFacebookUtilsV4 体系结构 armv7 的重复符号