Using Swift with Salesforce Mobile SDK
Creating Swift Apps
To create Mobile SDK
Swift projects, you can assemble the pieces manually, or you can use the forceios
create command. With forceios, specify native_swift as
the application type, or simply press Return. For
example:
For
manual app creation steps, see Creating an iOS Swift Project Manually.
1forceios create
2Enter your application type (native, native_swift, leave empty for native_swift): <press RETURN>
3Enter your application name: TestSwift
4Enter your package name: com.bestapps.swift
5Enter your organization name (Acme, Inc.): BestApps, Inc.
6Enter output directory for your app (leave empty for the current directory): TestSwiftSwift API Naming
In Mobile SDK, Swift
methods and classes closely follow the related Objective-C API signatures. However, to
facilitate calling Objective-C APIs, Mobile SDK defines
Swift-friendly names for most methods and parameters. When Mobile SDK defines a
custom Swift name, it appears in the Objective-C header file with the tag NS_SWIFT_NAME immediately following the Objective-C method
declaration. Here’s an example method
declaration:
With
class and protocol names, the declaration comes before the class
declaration:
If
an Objective-C API is not available in Swift, you’ll see the tag NS_SWIFT_UNAVAILABLE("") next to the API declaration.
1- (nullable SFSyncState*) reSyncByName:(NSString*)syncName
2 updateBlock:(SFSyncSyncManagerUpdateBlock)updateBlock
3 NS_SWIFT_NAME(reSync(named:onUpdate:));1NS_SWIFT_NAME(RestClient)
2@interface SFRestAPI : NSObjectMost Swift class names are simply the Objective-C name without the “SF” prefix, but there
are some refinements as well. For example, SFRestAPI
equates to RestClient in Swift. Here are some
noteworthy examples.
| Objective-C | Swift |
|---|---|
| SalesforceSDKManager | SalesforceManager |
| SFUserAccountManager | UserAccountManager |
| SFRestAPI | RestClient |
| SFSmartStore | SmartStore |
| SFMobileSync* | Mobile Sync* |