+ Start a Discussion
Kahlil DozierKahlil Dozier 

Native iOS development in Swift- how to post a comment using SFRestRequest and Chatter API?

In Objective C, I follow the steps as outlined here and it works fine: https://developer.salesforce.com/docs/atlas.en-us.noversion.mobile_sdk.meta/mobile_sdk/ios_rest_apis_using_methods.htm?search_text=forceios

But when following the same steps translating the code into Swift, I run into an issue with the SFRestRequest initializer, whose signature is below:
 
Public convenience init(method: SFRestMethod, path: String, queryParams: [String : String]?)

As you can see, queryParams takes a Swift Dictionary with a key and value of the String type.  But when you follow the example, it seems you end up with a Dictionary with a key of the String type and value of the Dictionary type; and thus we have a type mismatch.

This doesn't seem to be a problem in Objective C, because upon inspection of the method signature in SFRestRequest.m, queryParams appears to be able to take a generic NSDictionary: 
 
+ (instancetype)requestWithMethod:(SFRestMethod)method path:(NSString *)path queryParams:(NSDictionary *)queryParams

For reference here is the Swift code I'm trying out:
 
//build the queryParams dictionary from a JSON String
let body: String = "{ \"body\" :{\"messageSegments\" :[{ \"type\" : \"Text\",\"text\" : \"My Comment\"}]}}"
        let queryParams = SFJsonUtils.objectFromJSONString(body) as! [String:AnyObject] 
//construct and send the request
        let request = SFRestRequest(method: SFRestMethod.POST, path: "/services/data/v36.0/connect/communities/my_community_ID/chatter/feed-elements/my_element_ID/capabilities/comments/items", queryParams: queryParams as? [String:String])
        SFRestAPI.sharedInstance().send(request, delegate: self)

But unwrapping queryParams fails and resolves to nil.  Trying to forcefully cast queryParams to [String:String] does not work either, and just causes a crash.

How might I get around this?
 
Best Answer chosen by Kahlil Dozier
Kahlil DozierKahlil Dozier
So what I did was to follow the directions outlined in http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift to create an Objective-C file that my Swift code could call.  

Below are my Objective-C interface/implementation files:

SFRestRequestSending.h:
 
//
//  SFRestRequestSending.h
//

#import <Foundation/Foundation.h>
#import <SalesforceRestAPI/SalesforceRestAPI.h>
#import <SalesforceSDKCore/SalesforceSDKCore.h>
/*This is an Objective-C wrapper class that exists to allow swift classes to use the requestWithMethod:path:queryParams: method of the SFRestRequest class whenever we have a non-nil value for queryParams.  Currently, this method plays fine accepting generic NSDictionaries in Objective-C, but requires a Dictionary specifically typed [String:String] in Swift.  This might be a bug in the SalesforceRestAPI framework, because we need to pass generic swift dictionaries to this method to form some requests.*/
@interface SFRestRequestSending : NSObject

@property(nonatomic,strong) SFRestRequest* restRequest;
/*
 parameter "method" is an NSInteger which refers to the http request method:
 
 GET = 0
 POST = 1
 PUT = 2
 DELETE = 3
 HEAD = 4
 PATCH = 5 */

//standard SFRestRequest
-(id)initWithMethod:(NSInteger)method path:(NSString*)path queryParams:(NSDictionary*)queryParams;

 SFRestRequestSending.m:
 
//
//  SFRestRequestSending.m
//

#import "SFRestRequestSending.h"
#import "My_Project_Name-Swift.h"

@implementation SFRestRequestSending

-(id)initWithMethod:(NSInteger)method path:(NSString *)path queryParams:(NSDictionary *)queryParams{
    self = [super init];
    if(self){
        self.restRequest = [SFRestRequest requestWithMethod:method path:path queryParams:queryParams];
    }
    return self;
}
@end

Replace "My_Project_Name" with the name of your Xcode project, as specified in the stack overflow directions. 

Then, from swift, I would call this class like:
 
//compose and execute a GET request
let requestWrapper = SFRestRequestSending(withMethod: 0, path: myPath, queryParams: paramDict);

let request = requestWrapper.restRequest;

SFRestAPI.sharedInstance().send(request, delegate: myDelegate);

 

All Answers

Kahlil DozierKahlil Dozier
Just as an aside, I have achieved a temporary workaround: I created an objective-C class whose only purpose is to call the SFRestRequest method in objective-C, and then call this code from my Swift code.  It works, but it still doesn't solve the problem that I am unable to call this method in Swift. 
Niklas AlvaeusNiklas Alvaeus
I have the same problem, still even after the new 5.0

Are you abel to share your objective-c class please?
 
Kahlil DozierKahlil Dozier
So what I did was to follow the directions outlined in http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift to create an Objective-C file that my Swift code could call.  

Below are my Objective-C interface/implementation files:

SFRestRequestSending.h:
 
//
//  SFRestRequestSending.h
//

#import <Foundation/Foundation.h>
#import <SalesforceRestAPI/SalesforceRestAPI.h>
#import <SalesforceSDKCore/SalesforceSDKCore.h>
/*This is an Objective-C wrapper class that exists to allow swift classes to use the requestWithMethod:path:queryParams: method of the SFRestRequest class whenever we have a non-nil value for queryParams.  Currently, this method plays fine accepting generic NSDictionaries in Objective-C, but requires a Dictionary specifically typed [String:String] in Swift.  This might be a bug in the SalesforceRestAPI framework, because we need to pass generic swift dictionaries to this method to form some requests.*/
@interface SFRestRequestSending : NSObject

@property(nonatomic,strong) SFRestRequest* restRequest;
/*
 parameter "method" is an NSInteger which refers to the http request method:
 
 GET = 0
 POST = 1
 PUT = 2
 DELETE = 3
 HEAD = 4
 PATCH = 5 */

//standard SFRestRequest
-(id)initWithMethod:(NSInteger)method path:(NSString*)path queryParams:(NSDictionary*)queryParams;

 SFRestRequestSending.m:
 
//
//  SFRestRequestSending.m
//

#import "SFRestRequestSending.h"
#import "My_Project_Name-Swift.h"

@implementation SFRestRequestSending

-(id)initWithMethod:(NSInteger)method path:(NSString *)path queryParams:(NSDictionary *)queryParams{
    self = [super init];
    if(self){
        self.restRequest = [SFRestRequest requestWithMethod:method path:path queryParams:queryParams];
    }
    return self;
}
@end

Replace "My_Project_Name" with the name of your Xcode project, as specified in the stack overflow directions. 

Then, from swift, I would call this class like:
 
//compose and execute a GET request
let requestWrapper = SFRestRequestSending(withMethod: 0, path: myPath, queryParams: paramDict);

let request = requestWrapper.restRequest;

SFRestAPI.sharedInstance().send(request, delegate: myDelegate);

 
This was selected as the best answer
Niklas AlvaeusNiklas Alvaeus
Oh yes, many thanks, you saved my day. Not sure why this is not sorted in v5.0. Anyhow, able to use it now via this method. Thanks again.
quickbooks supportquickbooks support
Thanks for sharing this post with us we all like it and waiting for more post. To know more visit our site.https://quickbooksupports.co/
Montu raiMontu rai

Oh yes, many thanks, you saved my day. Not sure why this is not sorted in v5.0. Anyhow, able to use it now via this method. Thanks again.

https://apkwallet.com/turbo-bomber-apk-download/
Montu raiMontu rai
Oh yes, many thanks, you saved my day. Not sure why this is not sorted in v5.0. Anyhow, able to use it now via this method. Thanks again.

turbo bomber apk  (https://apkwallet.com/turbo-bomber-apk-download/)
Kimber lyyKimber lyy
Hi, thanks for this valuable knowledge. Sassy captions(https://selfiecaptions.org/sassy-instagram-captions.html)
Aviraj DhadhalAviraj Dhadhal
<a href="https://whatsappstatusclub.in">Whatsapp Status Video</a>
 
Aviraj DhadhalAviraj Dhadhal
https://whatsappstatusclub.in
Quote StatusQuote Status
rocky dasrocky das
Manoj kumar 731Manoj kumar 731

Good info

 

Self Respect Quotes That Will Highlight Your Worth (https://www.wewishes.com/self-respect-quotes-that-will-highlight-your-worth/)

Michael Jackson Quotes To Rock And Roll Your Life (https://www.wewishes.com/michael-jackson-quotes-to-rock-and-roll-your-life/)

Where to Buy Chocolate Covered Strawberries​​​​​​​ (https://www.wewishes.com/where-to-buy-chocolate-covered-strawberries/)

Apoorva Verma 6Apoorva Verma 6
Check Out - Life Quotes In Hindi
 Life Quotes That'll Motivate You to Take That Next Step. Refocus and recharge with these inspirational sayings. 'What are you doing for others?' The quality, not the longevity, of one's life, is what is important. Life imitates art far more than Art imitates Life. Find ecstasy in life; the mere sense of living is joy enough.
YD98YD98
Also I have this problem. I can't still fix it 
ami bossami boss
Yes right. I will help you with a question. Amazing content (https://latestcelebsbio.com/). love this valuable article increases me more to know to keep it up.

https://latestcelebsbio.com/
ravi sharma 148ravi sharma 148
Yes right. I will help you with a question. hdmovieshub (https://hdmovieshub.buzz/). love this valuable article increases me more to know to keep it up.
Anil Kumar 1334Anil Kumar 1334
Butati dham naugar rajasthan ( https://newstram.com/butati-dham/ )
Sunny PaharSunny Pahar
This is an Amazing Post Thanks a Lot ....Btw I am a Gamer and Loves to play PC Games. Some of them are GTA 5, APEX LEGENDS, Resident Evil, The Evil Within, Honey Select (https://pcgamesd.com/honey-select-2-free-download/), Minecraft, and Many More.
chirag hankarechirag hankare
OBMMS- Apply, Application Status, Beneficiary 2020 (https://ihindihelp.com/obmms/) get all the information related to obmms
Santosh IyerSantosh Iyer
Here we get solutions for every technical problem related to IT applications. Government schemes (https://pinmypic.com) for education in information technology will allow young students to innovate things. 
Milan Patel 16Milan Patel 16
Good Morning Message In Hindi | Good Morning Quotes in Hindi (https://bloggingmafiya.com/good-morning-message-in-hindi-morning-quotes/)
Milan Patel 16Milan Patel 16
Best Quotes on Life in Hindi | Inspirational Quotes in Hindi (https://bloggingmafiya.com/quotes-on-life-in-hindi-inspirational-qoutes/)
Gyansora OfficialGyansora Official
Gyansora.com | Best Educatioal and Professional Career Orieanted website. http://en.gyansora.com/
Gyansora OfficialGyansora Official