Swift - Calling initializer, missing argument - ios

I am use Swift to call an initializer from objective-c class(JSONModel)
The class contains some initializer:
-(instancetype)initWithString:(NSString*)string error:(JSONModelError**)err;
-(instancetype)initWithString:(NSString *)string usingEncoding:(NSStringEncoding)encoding error:(JSONModelError**)err;
-(instancetype)initWithDictionary:(NSDictionary*)dict error:(NSError **)err;
-(instancetype)initWithData:(NSData *)data error:(NSError **)error;
Since I want to call the initWithDictionary one, I write code like
var error:NSError
var loginRes = RegisterResponse(dictionary: dict , error: &error)
in which , RegisterResponse is a subclass of JSONModel, dict is a pre-used Dictionary variable
However, the complier complains like:
missing argument for parameter "usingEncoding" in call.
It seems that the complier think I am calling the
-(instancetype)initWithString:(NSString *)string usingEncoding:(NSStringEncoding)encoding error:(JSONModelError**)err;
rather than the third one. Are there any solution?

-(instancetype)initWithDictionary:(NSDictionary*)dict error:(NSError **)err;
is mapped to Swift as
init!(dictionary dict: [NSObject : AnyObject]!, error err: NSErrorPointer)
As explained in Adopting Cocoa Design Patterns / Error Reporting,
you have to pass an optional NSError as an in-out expression:
var error : NSError?
let loginRes = RegisterResponse(dictionary: dict, error: &error)
or with error checking:
var error : NSError?
if let loginRes = RegisterResponse(dictionary: dict, error: &error) {
println("success")
} else {
println(error!.localizedDescription)
}

Related

Error after converting objective-c code to swift 3.0.2 Google Drive Rest API

The problem is quite simple.I have a code that works fine in Objective-C, but I need the same code to work in swift.With the code below I am uploading file named"photo.png" to my Google Drive from Documents folder on iPhone.(I deleted the part with setting Documents directory folder to save some space)
Here is the original code:
-(void)uploadPhoto{
metadata.parents = ids;
GTLRUploadParameters *uploadParameters;
uploadParameters = [GTLRUploadParameters uploadParametersWithData:photoData MIMEType:#"image/png"];
uploadParameters.shouldUploadWithSingleRequest = TRUE;
GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:metadata
uploadParameters:uploadParameters];
query.fields = #"id";
[self.service executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
GTLRDrive_File *file,
NSError *error) {
if (error == nil) {
NSLog(#"File photo ID %#", file.identifier);
} else {
NSLog(#"An error occurred: %#", error);
}
}];
}
Most of it I got form Google developers.IN Objective - Its working fine!
Here is what In got in swift:
func uploadPhotos()
{
var ids : NSArray!
metadata.parents = ident as! [String]?
var uploadParameters = GTLRUploadParameters(data:photoData! as Data,mimeType:"image/png")
uploadParameters.shouldUploadWithSingleRequest = true
var query :GTLRDriveQuery_FilesCreate
query = GTLRDriveQuery_FilesCreate.query(withObject: metadata, uploadParameters: uploadParameters)
query.fields = "id"
let vc:DriveListTableViewController
self.service.executeQuery(query,completionHandler :{(ticket: GTLRServiceTicket!,
file: GTLRDrive_File!,
error: Error!)-> Void in
if error == nil {
print("File photo ID \(file.identifier)")
}
else {
print("An error occurred: \(error)")
}
})
}
The Error I get is in the the line with self.service.exequteQuery
swift:314:59: Cannot convert value of type '(GTLRServiceTicket!, GTLRDrive_File!, Error!) -> Void' to expected argument type 'GTLRServiceCompletionHandler?'
Please help me solve that Error! Have tried different variants!
Refer with this SO thread. You may try changing the unwrapping of the parameters in the closure. Maybe this is required to match what the Objective-C implementation of the function is expecting.
You may also check this additional references:
Swift 2 to Swift 3: Cannot convert value of type '(Data?, NSError?) -> Void' to to expected argument type 'GTMSessionFetcherCompletionHandler?'
As per SE-0112, NSError is now bridged to Swift as the Error protocol. In fact, if you ⌥ + click on the GTMSessionFetcherCompletionHandler type in Swift, you'll see exactly how it's bridged:
typealias GTMSessionFetcherCompletionHandler = (Data?, Error?) -> Void
Swift error Cannot convert value of type '(AFHTTPRequestOperation?, AnyObject?) -> ()
Cannot convert value of type (PFUser!, NSError) void to expected argument type PFUserResultBlock

NSJSONSerialization Extra argument in 'error' tutorial book IOS 8 SDK Development 2nd Edition [duplicate]

This question already has answers here:
Swift: Extra argument 'error' in call
(3 answers)
Closed 7 years ago.
From the book IOS 8 SDK Development 2nd Edition by Chris Adamson on the end of Chapter 6.
I have an issue with the error call and how exactly I convert it from the old swift to the new swift, with do.. and try here's my block of code
func handleTwitterData (data: NSData!, urlResponse: NSHTTPURLResponse!, error: NSError!) {
if let dataValue = data {
var parseError : NSError? = nil
let jsonObject : AnyObject? = NSJSONSerialization.JSONObjectWithData(dataValue, options: NSJSONReadingOptions(0), error: &parseError)
print("JSON error: \(parseError)\nJSON response: \(jsonObject)")
} else {
print("handleTwitterData received no data")
}
}
In swift 2.0 you will not use error parameter.
If objective-c function has last parameter as NSError**, swift 2.0 remove it and mark it as a function that can throw exception.
So you do not need to write that parameter, but need to use swift exceptions syntax instead.
do {
let jsonObject : AnyObject? = try NSJSONSerialization.JSONObjectWithData(dataValue, options: NSJSONReadingOptions(0))
} catch {
print("\(error)")
}

Objective-C to Swift framework throwing function conversion

I am using CHCSVParser to parse some data. It has been installed through CocoaPods with use_frameworks!. Viewing the header file, there are two functions that I can use, I want to use the one that returns an error to see if something went wrong.
public convenience init!(contentsOfDelimitedURL fileURL: NSURL!, options: CHCSVParserOptions, delimiter: unichar)
// This is the one I want to use as I need the error if it fails.
public convenience init(contentsOfDelimitedURL fileURL: NSURL!, options: CHCSVParserOptions, delimiter: unichar, error: ()) throws
The actual Objective-C methods of the two above:
- (NSArray *)componentsSeparatedByDelimiter:(unichar)delimiter options:(CHCSVParserOptions)options;
- (NSArray *)componentsSeparatedByDelimiter:(unichar)delimiter options:(CHCSVParserOptions)options error:(NSError *__autoreleasing *)error;
I am using the error function like this:
do {
let components = try NSArray(contentsOfDelimitedURL: url, options: CHCSVParserOptions.RecognizesBackslashesAsEscapes, delimiter: 0x002C, error: ())
// Do what ever I need to do...
} catch {
print("Error: \(error)")
}
However I assume passing () to the error param is wrong. When I run this with something I know will fail the catch is never being called. Unsure on how to call this function correctly?
Using Xcode 7 beta 5.
You need to pass the error like this
var error: NSError?
var results = context.executeFetchRequest(request, error: &error)
and also i think you need to check the error object for any error rather than putting try catch

Why do we use &error in Swift?

I commonly see a pointer to an optional error variable being used, just like in this block of code:
if fileManager.fileExistsAtPath(path)
{
var error: NSError?
if !fileManager.removeItemAtPath(path, error: &error)
{
println("Error removing the file : \(error)")
}
}
Why do we do this?
The error parameter is an inout parameter, and can set the value of error rather than returning it from the function. Look up "inout" in Apple's iBook on Swift.

Swift Error - extra argument 'option' in call for SimpleAuth

Hi I have been battling with a swift error as I cannot match the arguments of an objective-c method
SimpleAuth.authorize("instagram",
options: ["scope" : "likes"], completion: {
(responseObject : NSDictionary!, error : NSError!) -> Void in
self.accessToken = responseObject["credentials"]["token"]
......
})
//error Extra argument 'options' in call
Declared as:
+ (void)authorize:(NSString *)provider options:(NSDictionary *)options completion:(SimpleAuthRequestHandler)completion;
Code completion is :
SimpleAuth.authorize(<#provider: String!#>, options: <#[NSObject : AnyObject]!#>, completion: <#SimpleAuthRequestHandler!##(AnyObject!, NSError!) -> Void#>)
I have tried type casting, down casting, declaring as a separate variables, but still cant get it to work.
Any ideas, will be much appreciated
It's your definition of your completion block. SimpleAuthRequestHandler is defined as:
typedef void (^SimpleAuthRequestHandler) (id responseObject, NSError *error);
But your completion block/closure is defined as:
(responseObject : NSDictionary!, error : NSError!) -> Void in
You can't just change the type from id (AnyObject in Swift) to NSDictionary! without explicitly casting it. Your call should look something like this:
SimpleAuth.authorize("instagram", options: ["scope" : "likes"], completion: {
(responseObject : AnyObject!, error : NSError!) -> Void in
/* ... */
})
You can then make responseObject an NSDictionary with a cast:
var response = responseObject as NSDictionary

Resources