2

I am using objective-git with Swift and cannot compile the GTRepository.createCommitWithTree method.

The method can optionally be called without author : GTSignature and committer : GTSignature parameters.

I'm new to Swift and overloading functions. I'd like to know how to structure this so it will compile.

My code uses all the types specified in the objective-git method:

func commitTree ( tree : GTTree ) {

    let message : NSString
    let author : GTSignature
    let committer : GTSignature
    let parents : NSArray
    let updatingReferenceNamed : NSString
    var error : NSError?

    GTRepository.createCommitWithTree( tree, message, author, committer, parents, updatingReferenceNamed, &error )
}

In this code, compiler cannot invoke method with an argument list of these types. Compiler provides additional information: "Overloads for 'createCommitWithTree' exist with these partially matching parameter lists: (GTTree, message: String, author: GTSignature, committer: GTSignature, parents: [AnyObject]?, updatingReferenceNamed: String?), (GTTree, message: String, parents: [AnyObject]?, updatingReferenceNamed: String?)"

If I refactor to use the types suggested above, compiler won't compile with "Ambiguous reference to member 'createCommitWithTree'"

How do I write this to compile?

Thanks

almccann
  • 389
  • 3
  • 13

1 Answers1

0

Not sure if you're still having trouble with this (ran across this old unanswered question when searching for something else), but there are several possible problems with your code as posted. First off, you will not be passing an NSError by reference (assuming you're using Swift 2). Additionally, this method is an instance method for GTRepository, not a class method, so you have to call it on an instance of the repo. Here's the way this sort of method translates into Swift:

// These all need to be defined somewhere earlier for the following code to function
let repo: GTRespository
let tree: GTTree
let commitMsg: String
let parentCommit: GTCommit?
let refName: String?

// And here's the actual code you would execute
var createdCommit: GTCommit?
do {
    try createdCommit = repo.createCommitWithTree(tree, message: commitMsg, parents: parentCommit?, updatingReferenceNamed: refName?)
} catch let error as NSError {
    NSLog("Error creating commit: \(error.domain)")
}
if createdCommit != nil {
    // Do something with your new commit
}

Good luck! Translating Objective-Git into Swift can get kind of weird thanks to the extensive use of pass-by-reference NSErrors and the occasional bool, but this particular method should be pretty straight forward to use as long as you wrap it up with do/try/catch.

One Crayon
  • 19,119
  • 11
  • 33
  • 40