3

Note:

Here's some thoughts from CA at Parse about this:

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

(notice how popular that question is - hot topic). Hope it helps someone


Here's an iOS7 Parse cloud code call ...

how to do this in SWIFT ? cheers

To be clear ... can you use "callFunctionInBackground" in SWIFT, or do you have to just call to an objc class?

-(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
    }

Note, I've noticed this is a google landing page for trying to figure out "how the heck to do cloud code calls" (unrelated to Swift). Here is a full, complete set of example code for both iOS and Android of custom cloud code functions, in the Parse.com universe https://stackoverflow.com/a/24010828/294884 Hope it helps someone

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719

2 Answers2

9

Add an Objective-C .m file to your project. Xcode will ask about creating a Bridge Header file. Say yes.

Delete the .m file. In the bridge header file, add your import statement:

#import <Parse/Parse.h>

Another answer, which has a way better walkthrough than mine: https://stackoverflow.com/a/24005242/353988

Now you can call native Parse code in Swift, i.e.:

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")
    })

  }

}
Community
  • 1
  • 1
Fosco
  • 38,138
  • 7
  • 87
  • 101
  • A perfect explanation! SO brilliance! I guess "callFunctionInBackground" or any other Parse call would work the same. – Fattie Jun 03 '14 at 19:55
  • 1
    Yep, just have to find the right syntax for the returning block. – Fosco Jun 03 '14 at 20:24
  • Awesome and just some more info from Awesome Parse Krew .. https://www.parse.com/questions/ios-when-will-swift-for-parse-be-ready – Fattie Jun 12 '14 at 08:29
  • Added an opening bracket following saveInBackgroundWithBlock – Carl Sep 01 '14 at 10:48
-2

It is not possible to call this exact function (callFunctionInBackground) because it is an obj-C function.

Please refer to this question how to call obj-c functions.

With time Parse will also introduce Swift implementation.

Community
  • 1
  • 1
Nat
  • 12,032
  • 9
  • 56
  • 103
  • Ahhhh ..you're saying it's **not possible** to call to callFunctionInBackground in SWIFT .. do I understand you Vive? Cheers... – Fattie Jun 03 '14 at 09:49
  • It is not possible to call this exact function because it is obj-C function ;). However you can import obj-C class to your Swift code and implement Swift method to work in the background basing on [this link](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75). Alternatively you can write this in obj-C, as you have, then import it to your Swift code, but 1st method seems better. – Nat Jun 03 '14 at 10:01
  • Sorry but this is wrong. It's easy to access Objective-C libraries in Swift projects. – Fosco Jun 03 '14 at 18:55
  • @Fosco I don't claim anything else. I've said that you have to import Objective-C and implement a method where you use Objevtive-C code from that imported file. Also I've attached a link, where you have exact explanation how to access Obj-C methods in Swift so I don't understand your vote down. – Nat Jun 05 '14 at 06:41
  • The link is definitely awesome, but it's preceded by 'not possible' and followed by 'with time'. – Fosco Jun 05 '14 at 06:47