Xcode 9.1 Refactoring not available - swift

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.

Related

Xcode - how to use a test plan for a scheme (convert button is missing)

I would like to use test plans for the scheme in one of my libraries. In a previous library I created, I selected the "Convert to use Test Plans..." button in the scheme editor, but in my current project, this button is missing. In addition, after creating a test plan, it does not show up in Product > Test Plan in the menu bar.
In a different project that I created, the "Convert to use Test Plans..." button exists:
Why is this button missing in one of my projects? What do I need to do so that I can add a test plan to my scheme?
Answer to the above question 'Why is the Convert to use Test Plans missing in one of my projects?
I also had this issue and boiled it down to the following:
It depends on if there is this element in your MyProject.xcscheme file:
<TestPlans>
</TestPlans>
If you have this empty element in there, the button won't be shown.
This might happen like this:
create a new project
open the app scheme -> the button is there
now use the convert button and create a xctestplan
take a look at the changes in your git client
we see that the element has been removed and the elements has been added. Ok for that.
now remove the testplan in the schema -> the convert button does not appear again. This is because now we have the empty element. Appearently Xcode doesn't react correctly - I think this is a bug.
Workaround: remove the
<TestPlans>
</TestPlans>
and bring back the
<Testables>
</Testables>
I use VS Code for this because the xcschema is in a subfolder of the .xcodeproj which is easier to handle with VS Code
UPDATE: seems like it's fine to just delete the element

Compiling swift source files stuck

I have been working on my project when suddenly I have not compiled anymore, it stays waiting in Compile swift source files ... I cleaned the project, the folder, deleted the app and nothing. Is there any way to know what is happening?
Is there any way to know what is happening?
Yes. Go into the Report Navigator which is the one selected by the little speech bubble at the top right of the navigator pane. It will give you a list of all the actions you have performed like builds and tests and runs. The current build will be at the top of the pane. Click on it.
In the main editor you will see some buttons at the top and maybe some messages. Click the All Messages button and it will show you a tree view of the current build. The bottom line will be what the build system is doing now. If it is not changing (i.e. new lines appearing) the build is stuck.
Almost certainly the bottom line will be something like Compile SomeFile.swift. Assuming it is, you can now triage the problem. The Swift compiler usually gets stuck on inferring types for complicated expressions. The way to triage this is to comment out the entire file and then add lines back until the problem reappears. Make sure that you always add enough back so that the file compiles without error.
Obviously, the last line you added back will be the one causing the problem. Usually it's an expression that concatenates strings together with non strings that causes the problem.
The problem is in dictionary [String:Any] when I change the dictionary to [String:String] the problem is resolved . Is very strange..

Eclipse working sets -- how to rename them? Also, can the error-icon be disabled?

After mild frustration with the difficulty to make top-level "plain old folders" within Eclipse for visual-organization purposes, I discovered that the thing I'm after is called a "working set". Hooray! But they don't seem to be rename-able, by any of the apparent avenues (right-clicking on it or using the Configure Working Sets window).
Is that just the way things are, since no one should be so lazy as to refuse making a new working set with the right name and transferring everything over? Or am I missing something obvious?
I also have a more minor question whose answer I already think I know. Can I tell a specific working set not to change its icon to have the "red X" when one of its children has an error? Nothing in the preferences under Debugging suggests to me the ability to turn off the automatic icon-changing. It's a useful feature, but I have a few simple practice projects with very basic errors, and I don't need the visual reminder to "fix" them, especially if they're in my "Practice" working set, whose icon I'd prefer not to change.
To rename a Working Set, you need to get to the dialogue of selecting a Working Set (click on the white down arrow at the top right of the package explorer > Configure Working Sets..), focus on your Working Set and click the "Edit" button. There, you can change the Working Set's name, as well as what's actually included in the Working Set.
There is no way (that I know of) to change the icon display to avoid showing the errors marker.

Xcode's Refactor menu is always disabled

For some reason Xcode's Refactor is always disabled. I used to be able to activate it via selecting a class name and right clicking in the editor to select it. But now no matter what I do, it remains disabled. Any idea what's going on?
Working with Xcode 5, I kept getting "Can't refactor during indexing" when trying to refactor. Rebuilding the index seemed to hang at "Indexing - Waiting for make" forever.
I then noticed that make was already used by another process on my machine (not by Xcode). Shutting this process down enabled Xcode to finally finish indexing
Your project's index might be out of date/corrupt. In the General tab of the Project Settings window, click Rebuild Code Index and wait until it's done.
You can check the progress of the rebuild in the Activity window (Window > Activity).
I just ran into a similar problem in Xcode. I could not refactor anything in the class's .m (implementation file). The only explanation I could come up with is that it was not indexing the file for some reason, and I could not force it to reindex. The only hack that I found that worked was to copy the file to temporary location, remove it from my Xcode project, copy the file from the temp location back to my project's location, and then re-add the file via Xcode.
None of these suggestions work for Xcode 3.2 - the ONLY way is to select the file you want to rename in the left Groups & Files column. THEN highlight the name of the .m or .h in the right code view, then you can right click and select Refactor!
For instance if you're changing the DetailViewController name, highlight the actual DetailViewController.h word in code.
Note, you should also always make sure you've selected the .h file, not the .m
I faced the same thing in Xcode 11. and this worked for me:
I changed iPhone 8 simulator to generic iOS Device and it worked when it is on Generic
I'm new in iOS development,I have met this similar problem.
Xcode:Version 7.1 (7B91b),it looks like Refactor not working:
But when I put my mouse on the red circle point in the following picture,and then the Refactor working.
I think this is a low level mistake,but may be a reference for someone.

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!