【问题标题】:How to make this Parse.com cloud code call?如何进行 Parse.com 云代码调用?
【发布时间】:2014-06-03 09:01:45
【问题描述】:

注意:

以下是 Parse 的 CA 对此的一些想法:

https://www.parse.com/questions/ios-when-will-swift-for-parse-be-ready

(请注意这个问题的受欢迎程度 - 热门话题)。希望它可以帮助某人


这是一个 iOS7 Parse 云代码调用...

如何在 SWIFT 中做到这一点?干杯

明确一点...你可以在 SWIFT 中使用“callFunctionInBackground”,还是只需要调用一个 objc 类?

-(void)tableView:(UITableView *)tableView
        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
        forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    int thisRow = indexPath.row;
    PFUser *delFriend = [self.theFriends objectAtIndex:thisRow];

    NSLog(@"you wish to delete .. %@", [delFriend fullName] );

    // note, this cloud call is happily is set and forget
    // there's no return either way. life's like that sometimes

    [PFCloud callFunctionInBackground:@"clientRequestFriendRemove"
            withParameters:@{
                            @"removeThisFriendId":delFriend.objectId
                            }
            block:^(NSString *serverResult, NSError *error)
            {
            if (!error)
                {
                NSLog(@"ok, Return (string) %@", serverResult);
                }
            }];

    [self back];    // that simple
    }

注意,我注意到这是一个谷歌登录页面,用于试图弄清楚“如何进行云代码调用”(与 Swift 无关)。这是一套完整的适用于 iOS 和 Android 的示例代码自定义云代码功能,在 Parse.com 宇宙中https://stackoverflow.com/a/24010828/294884希望它可以帮助某人

【问题讨论】:

标签: ios parse-platform swift


【解决方案1】:

将 Objective-C .m 文件添加到您的项目中。 Xcode 将询问有关创建桥头文件的问题。说是。

删除 .m 文件。在桥头文件中,添加您的导入语句:

#import <Parse/Parse.h>

另一个答案,它的演练比我的要好:https://stackoverflow.com/a/24005242/353988

现在您可以在 Swift 中调用原生 Parse 代码,即:

import UIKit
import Foundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    Parse.setApplicationId("appid", clientKey: "clientkey")
    var obj = PFObject(className:"TestObject")
    obj.setObject("bar", forKey: "foo")
    obj.saveInBackgroundWithBlock ({ 
      (succeeded: Bool!, err: NSError!) -> Void in
      NSLog("Hi")
    })

  }

}

【讨论】:

  • 完美的解释!如此辉煌!我猜“callFunctionInBackground”或任何其他 Parse 调用都会起作用。
  • 是的,只需要找到返回块的正确语法。
  • 太棒了,还有来自 Awesome Parse Krew 的更多信息 ..parse.com/questions/ios-when-will-swift-for-parse-be-ready
  • 在 saveInBackgroundWithBlock 之后添加了一个左括号
【解决方案2】:

不可能调用这个确切的函数 (callFunctionInBackground),因为它是一个 obj-C 函数。

如何调用obj-c函数请参考this question

随着时间的推移,Parse 还将引入 Swift 实现。

【讨论】:

  • Ahhhh ..你是说在 SWIFT 中调用 callFunctionInBackground 是不可能 ..我明白你的意思吗?干杯...
  • 不可能调用这个确切的函数,因为它是 obj-C 函数 ;)。但是,您可以将 obj-C 类导入到您的 Swift 代码中,并根据 this link 实现 Swift 方法以在后台工作。或者,您可以像以前一样在 obj-C 中编写它,然后将其导入您的 Swift 代码,但第一种方法似乎更好。
  • 抱歉,这是错误的。在 Swift 项目中访问 Objective-C 库很容易。
  • @Fosco 我没有其他要求。我说过你必须导入 Objective-C 并实现一个方法,在该方法中你使用导入文件中的 Objevtive-C 代码。我还附上了一个链接,您可以在其中准确解释如何在 Swift 中访问 Obj-C 方法,所以我不明白您是否投反对票。
  • 这个链接确实很棒,但它前面是'not possible',后面是'with time'。
猜你喜欢
  • 2015-08-08
  • 2016-05-04
  • 1970-01-01
  • 2014-08-31
  • 2014-11-23
  • 2016-02-08
  • 2016-02-24
  • 2013-06-23
  • 1970-01-01
相关资源
最近更新 更多