How to make Xcode put starting brace on new line in Swift? - swift

I have noticed that the coding style in Swift appears to be to put braces on the same line as a method signature or if statement etc. Without getting into a debate about which is right or wrong, my aim is simply tom get Xcode to put the braces on a new line when it does its auto completion and for the new file templates.
I've tried the advice here and it doesn't work: https://forums.developer.apple.com/thread/23087
I'm using Xcode 8.1 on Sierra.
Has anyone else been able to get this to work?
Thanks,
Alan

You can use below methods in conjunction to accomplish this task.
ClangFormat and Uncrustify
Above method will only change the default statement templates and not the inbuilt methods formatting. (i.e autofilled viewDidLoad() method will still have the braces in the same line.) For this you should consider using these plugins. They have options to save the file with specified format.
(UPDATE Snippet Edit no longer works)
Snippet Edit
Snippet Edit is a small program that is used to edit the Xcode's standard code snippets. We have to do this way because there is no direct way to change the code style in Xcode settings and XCCodeSenseFormattingOptions is not supported since Xcode 4
You can follow below screenshot for reference. Remember to restart Xcode once you are done with editing.

Related

Xcode 10.1 highlighting off by one (or more) lines in Version Editor for storyboard file

When I use Version Editor to compare changes, or when committing changes, the changed lines are showing incorrectly (each one is showing offset by one or more lines). Typically I only experience this when opening my storyboard file, rather than code files. Does anyone know the cause and/or how to fix? Thanks
EDIT: This issue persists for me in Xcode 10.2
OK I discovered what had been causing this for me. I had been putting carriage returns into strings in interface builder using CTRL-RETURN, when I should have been using OPTION-RETURN to do so. Changing those removed the issue for me. Still I think this is something that could be handled in a more forgiving and user-friendly way by Xcode.

Swift Brackets to Java Brackets [duplicate]

I have noticed that the coding style in Swift appears to be to put braces on the same line as a method signature or if statement etc. Without getting into a debate about which is right or wrong, my aim is simply tom get Xcode to put the braces on a new line when it does its auto completion and for the new file templates.
I've tried the advice here and it doesn't work: https://forums.developer.apple.com/thread/23087
I'm using Xcode 8.1 on Sierra.
Has anyone else been able to get this to work?
Thanks,
Alan
You can use below methods in conjunction to accomplish this task.
ClangFormat and Uncrustify
Above method will only change the default statement templates and not the inbuilt methods formatting. (i.e autofilled viewDidLoad() method will still have the braces in the same line.) For this you should consider using these plugins. They have options to save the file with specified format.
(UPDATE Snippet Edit no longer works)
Snippet Edit
Snippet Edit is a small program that is used to edit the Xcode's standard code snippets. We have to do this way because there is no direct way to change the code style in Xcode settings and XCCodeSenseFormattingOptions is not supported since Xcode 4
You can follow below screenshot for reference. Remember to restart Xcode once you are done with editing.

Xcode 9.1 Refactoring not available

The refactoring menu in Xcode 9.1 is completely greyed out. I wanted to extract a method from existing swift code and got the following screen:
Do I have to enabled something special to get access to the new refactoring features?
Try cleaning your Xcode. Also try clearing out derived data and restart Xcode.
Clean Build --> (Command-Option-Shift-K)
Delete DerivedData folder in ~/Library/Developer/Xcode/DerivedData
Refactor option hides if you select version editor.
Thanks #paul
I had to change to the Active Schema to the one that matched the Target Membership of the file I was editing.
In other words:
In Project Navigator, select the file you're editing
Show the Inspectors panel on the right
Show the File Inspector
Observe the Target Membership values and set the Active Schema (at the top, by the Build and Stop buttons) to match.
Select text, right click, select Refactor
Observe all the Refactor options are now enabled.
This happened to me too and I could not find anything on it so...
My first post is born.
Mike T. hit on a small portion above (incomplete syntax).
Nothing is wrong. Xcode is amazing! It seems the Refactor/Extract feature is pretty smart. The option is not available if it does not make sense to extract a method.
It may not make sense to extract a method for many reasons. Xcode disables the feature when it encounters them in the selection. A few that I noticed are:
variable declarations
a solitary print statement
continue statements in the selection but I don't include the for loop container. (i'm impressed)
my guess is that there are a bunch of conditions...
Small thing for me was that the code I was trying to access was inside a closure so when I was selecting it and right clicking to Extract to a Method, it was greyed out. removing self on both instances, I could then select both lines to then extract to method
self?.activityIndicator.stopAnimating()
self?.activityIndicator.removeFromSuperview()
Both lines were inside a closure for a UIAlertAction. So removing self allows you to extract to a method in this case. Then just call self on the extracted method inside the closure and it should work.
self?.hideSpinner() //extracted method name
As other posters have already said, its best to make sure you're able to refactor what you want by selecting the right code.

How to enable automatic method suggestion in swift in Xcode?

i am learning swift programming language. Unlike objective-c , it does not have automatic method suggestion and variable replacement. Is there any trick or setting for automatic method suggestion in swift?
Thanks in advance.
If you're referring to autocomplete, Swift does have autocomplete, but it's buggy and sometimes breaks. If it's not working, try deleting the derived data for your project and letting it re-index. The directory should be at ~/Library/Developer/Xcode/DerivedData/

Bypassing Lock on Generated Code in NetBeans 6.5?

In NetBeans the GUI Builder generates code that is off limits to you from the editor.
I know you can switch to Design Mode and specify custom code and then enter edit in indirectly that way, but it's a pain to not be able to edit text that's 2 lines from your cursor.
Can I allow editing within those regions... I promise I'll be careful :)
Thanks.
Note: I don't want to lose code generation
If you open it in an external editor, there are two possibilities:
there is no guarantee that the changes you make will work (and not break anything) in the Netbeans UI Editor
The next time you edit the file in Netbeans it will be regenerated.
Either way, probably not what you want. The best thing to do would probably be to make the whole design up in the gui editor and when you are happy with it, delete the comments and continue on.
[sarcasm]
With the drag and drop gui editor, what could you possibly want to change by hand anyway?
[/sarcasm] ;)
If you open the .java files in Notepad or WordPad, you'll find pairs of comments that say
// GEN-BEGIN: <name of section>
...
// GEN-END: <name of section>
Delete those comments, and two things will happen.
You'll be able to edit anywhere in the file in NetBeans.
NetBeans will not be able to regenerate the file from the GUI editor.
So it can be done, but don't do it unless you really mean it.
EDIT: "Note: I don't want to lose code generation." Ok, let's see here.
You want to change what is generated.
You don't want to lose code generation.
You don't want to use the facilities NetBeans provides for adding custom code.
It sounds like you're wishing yourself trouble. :D
I had a same problem. I extended JPanel with my own panel class and I was not able to pack it with the rest of UI. First I searched the web and I found your question. I was not able to find elegant solution to this problem.
Therefore, I decided to create another java class and copy NetBeans created code into it. It worked like a charm.
I agree, it would be nice if we can modify existing code like in .NET.
I copy the initcomponents() method, create my own method newInitComponents() paste the generated code, and override what needs to be overridden before i distribute the code. That way when a change is made in the editor, you can still copy paste the code into your new method. You keep the functionality without costing you the ability to override where necessary. Just call newInitComponents() in your constructor rather than the original. This is not the most efficient way of doing things, but it does what you want.
You can also try moving the .form file in
(project name)\src\(name of JFrame/JPanel).form
NetBeans will now see it as a .java file.
If you did not edit anything necessary for the GUI editor, you can move the .form file back into the src folder.
Can you subclass the class and redefine the method where this code is? Of course then you'll have to maintain the method over time as you make changes to the GUI.
You can maybe also look into AspectJ. It will modify the byte-code at build time and make the changes you define. Not sure if this gives you enough control over what code to change.
If you use APT you can rewrite source code at compile time. This way you can overwrite the code that's there at the moment. You could keep the new code in the same Java file along with comments to explain what is going on. This tutorial makes it pretty easy to get started. Copy that code, get it working, then tweak it to your liking. To find out where in the code the annotated method is call getPosition() on the Declaration.
Go to design page and select any one of the fields. Right click on any selected field and choose Customizer Code , In Customizer Code change all default code to custom creation . Now you can edit your design code as you like...
Very simple... the crappy UTF-8 did not let me compile one JasperReport (everything done by GUI). So, I needed to change Project to iso-8859-1. Result? The project is messed up and some of the things to correct are in the blocked parts of the code. So, I really NEED to edit those automatically generated parts. When they are regenerated, they will be fine because the project is now iso-8859-1.
Step # 1 Close the project in NetBeans
Step # 2 Delete the .form file from the source directory of your project
Reopen the project and it will be editable now!