1

Here's an example from HockeyApp that generates the following code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { BITHockeyManager.sharedHockeyManager().configureWithIdentifier(API_KEY) BITHockeyManager.sharedHockeyManager().disableCrashManager = true BITHockeyManager.sharedHockeyManager().startManager() BITHockeyManager.sharedHockeyManager().authenticator.authenticateInstallation() return true } 

Notice the repetition of BITHockeyManager.sharedHockeyManager() in each call. I'm looking for a syntax that does something like (pseudo code):

with BITHockeyManager.sharedHockeyManager() do { .configureWithIdentifier(API_KEY) .disableCrashManager = true .startManager() .authenticator.authenticateInstallation() } 

Is there a way to do this in Swift?

Edit: After writing this question I noticed that Alamofire is using "chainable methods" , which actually is originally what I was looking for. This allows a neat syntax, as follows (code from http://nshipster.com/alamofire/):

Alamofire.request(.GET, "http://httpbin.org/get") .response { (request, response, data, error) in println(request) println(response) println(error) } 

To use the corresponding syntax with HockeyApp would require modifying the BITHockeyManager class.

2 Answers 2

6

I don't think that something like

with BITHockeyManager.sharedHockeyManager() do { .configureWithIdentifier(API_KEY) .disableCrashManager = true // ... } 

is possible in Swift, but what you could do is to define a generic function

func with<T>(item : T, closure : T -> Void) { closure(item) } 

which simply calls the closure with the given item as the argument, and then take advantage of the "trailing closure syntax" and "shorthand argument name" $0:

with( BITHockeyManager.sharedHockeyManager() ) { $0.configureWithIdentifier(API_KEY) $0.disableCrashManager = true // ... } 
Sign up to request clarification or add additional context in comments.

6 Comments

Good solution but I think this does not make the code more readable and maintainable... :-)
Well, it is just a suggestion and meant as a reply to "Is there a syntax to get rid of defining the manager?", which was originally in the question and later removed.
It's a good solution, and also answers the modified question.
... which makes me wonder if you can do the same with a prefix operator.
@MartinR: Yes, fully agreed. It is just my opinion that I would not choose that way due to the bad readabilty of the code. The solution itself is very good!
|
3

You can always use the following approach, which is a little bit shorter, but still requires repetition of the variable manager:

let manager = BITHockeyManager.sharedHockeyManager() manager.configureWithIdentifier(API_KEY) manager.disableCrashManager = true manager.startManager() manager.authenticator.authenticateInstallation() 

1 Comment

I guess this is the best you can get. I'd do it the same way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.