Swift / MongoDB Server-无法读取数据,因为它的格式不正确。

时间:2018-09-20 15:57:32

标签: ios swift xcode mongodb

我正在开发一个带有登录屏幕的简单应用程序。在我的视图控制器(LoginViewController)的代码中,我创建了以下函数,该函数应允许用户注册。但是,当我测试应用程序时,控制台会告诉我“由于格式不正确,无法读取数据。”

@IBAction func onSignUp(_ sender: Any) {
    let newUser = PFUser()

    newUser.username = usernameField.text as String!
    newUser.password = passwordField.text as String!
    newUser.signUpInBackground { (success: Bool, error: Error?) in
        if success {
            print("Created User")
        } else {
            print(error!.localizedDescription)
        }
    }
}

我怀疑即时通讯遇到某种类型的错误?文本字段是否需要字符串以外的其他数据类型?知道为什么会这样吗?有任何已知的解决方案吗?

1 个答案:

答案 0 :(得分:0)

这两行很奇怪:

        [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;

        // Request using shared Notification Center
        [center requestAuthorizationWithOptions:options
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  if (granted) {
                                      NSLog(@"Notification Granted");
                                  }
                              }];

        // Notification authorization settings
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
                NSLog(@"Notification allowed");
            }
        }];

        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        content.title = title;
        content.body = body;
        content.sound = [UNNotificationSound defaultSound];
        NSDate *date = [NSDate date];

        // Trigger with date
        NSDateComponents *triggerDate = [[NSCalendar currentCalendar]
                                         components:NSCalendarUnitYear +
                                         NSCalendarUnitMonth + NSCalendarUnitDay +
                                         NSCalendarUnitHour + NSCalendarUnitMinute +
                                         NSCalendarUnitSecond fromDate:date];
        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate
                                                                                                          repeats:NO];

        // Scheduling the notification
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Identifier" content:content trigger:trigger];

        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Something went wrong: %@",error);
            }
        }];

PFUser上的newUser.username = usernameField.text as String! newUser.password = passwordField.text as String! username变量是否隐式地解开了可选变量?即使那样,我们通常也不会在Swift中编写这样的代码。我们可能会按照以下方式写一些东西:

password

尝试