When working in Xcode, it's often nice to pull up unit test cases in an assistant editor. Currently, I've been selecting the tests file manually, but I see the assistant editor has an option for Test Classes.
I've tried to get this option to pull up my tests file automatically, but I can't seem to make it work. Is there some sort of configuration required for it? Does it just not work for swift projects?
The assistant editor can automatically open the companion file in many cases. However, afaik, there is no automatic setting for opening the unit test.
To get the behavior you want you can fuzzy search with open quickly ⌘+⇧+o then open the result in the assistant editor with ⌥+⏎.
You might also find use in open quickly ⌘+⇧+o then ⇧+⏎.
You can see more about "Working Efficiently with Xcode" here.
The shortcuts mentioned in other answers work great, but they don't make the assistant editor automatically work with test classes. Turns out it didn't work with swift code previously, but Apple just added support for this to Xcode 9:
A file with a base name of FooTest or FooTests is now considered a counterpart of a file with a base name of Foo for navigation and the Assistant Editor. (28981734)
So, with Xcode 9, the test target filename needs to have the same name as the main code file + either 'Test' or 'Tests' and the Xcode test classes assistant editor will work automatically.
It seems that on Xcode 8 it automatically setup the suitable test class in the assistant editor after you import the original class inside the test class.
#testable import YOUR_MODULE_NAME
class MyViewControllerTests: XCTestCase {
var myVC : MyViewController!
}
After that you will find MyViewControllerTests on the assistant editor under "Tests Classes".
Related
I'm trying to extract the interface from class using Context Menu on class name -> Refactor -> Extract interface.
Dialog window appears where I enter the interface name and press the "Refactor" button.
It looks for class usages and then nothing happens: no preview window opens, no new file with interface created.
Just the dialog window continues to be shown and that's it.
This happens on MacOS, PhpStorm 2022.2 and 2021.2
What should I do to make it generate a new interface from the existing class?
I tried to Invalidate Caches with different checked options and then with all checked options, but it doesn't solve problem
After that I removed all files from directories:
Settings: ~/Library/Application Support/JetBrains/
Plugins: ~/Library/Application Support/JetBrains/Plugins/
Caches: ~/Library/Caches/JetBrains/
Logs: ~/Library/Logs/JetBrains/
Problem was gone
Then I localized problem by restoring Plugins folder and trying to disable different plugins
At last it was "Yii2 Support" plugin unfortunately
I'm using Visual Studio for Mac and design my UI in a storyboard using Xcode interface builder.
I'm trying to create a custom class of a UILabel and usually I just select the control and add my custom class name in the class property in xcode, and when xcode synchronises with Xamarin, the custom class is generated as a new file in the xamarin project, but this has stopped working.
Actually is seems to have stopped syncing all together. Are there any logs that I can check to see why? I have restarted xcode. Visual Studio for Mac, cleaned the project, deleted the bin and obj folders from the active project and rebooted the machine without success.
Any tips is very much appreciated!
My answer is a workaround and not a true answer as this has never worked for me. I have always used Xamarin iOS Designer to create custom class, then after file generation, close xib/storyboard and reopen in Xcode Interface Builder. Double checking for [Register("ClassName"]` tag.
Select Label in iOS Designer:
View -> Pads -> Properties Add Class name in Class Field:
Should see custom class autogenerated in SolutionExplorer:
Ensure .designer.cs file has Register Tag:
Now you can close the .xib in VS for Mac and reopen in Xcode. It probably won't autocomplete. But any Outlets you create will be an instance of your custom class as well as any custom attributes specified in .cs files will apply at runtime. Not as convenient as creating the custom class with Xcode but at least now you can use your custom class with Xcode Interface Builder.
I love my XCode as well! Instead of doing what you used to do, you can
Open the file in Xcode interface builder as usual with the Label
Find and Open the ABCViewController.h file in the other XCode window.
Hold control on your keyboard, click on your label on the GUI and drag it to the ABCViewController.h file and it should prompt you to create an outlet and name the class.
Save and Quit, and you should see the new Properties in your ABCViewController.designer.cs file
There's definitely a new issue after the update as you can see here as well
I am working in a mobile app testing project. We do write feature files (like scenarios) in a simple Xcode editor as of now. What I want is if I click on any step written in my feature file, it should take me to its step definition which is written in another document and open that in that editor.
I don't know if this facility is available with Xcode editor but if it is, please let me know how I can use it.
Also I thought if Eclipse or any of its plugin could be used but I don't know how.
Please let me know if there is any Editor which could be used for that (selecting an step and it takes me to the page where it is defined). And also it should be able to get executed on my mac with terminal.
Thank you very much for any help. really!
P.S. I am using git (SourceTree, Xcode etc) and working for an iOS app.
I suppose you use standard XCTest framework for testing? The feature you're looking for is present in Calabash framework. See tutorial at https://www.natashatherobot.com/calabash-ios-tutorial/
I've diligently followed the Apple instructions to import a custom module into a playground, including the instructions here. And yet I get:
Playground execution failed: /var/folders/z3/kd0nj4ln1rgcpm8bdz7067wh0000gs/T/./lldb/1874/playground1.swift:7:8: error: no such module 'Foo'
import Foo
How can I recover to a working Playground import? [Edit: Note, two answers have produced detailed instructions to associate a playground with a framework; I have followed those instructions but no luck. The solution will need to involve reconfiguring something in Xcode; my installation is apparently broken]
Detailed images of the error and attempts:
Here is another attempt, based on the answer of #EricD, showing the directory structure, no dice.
For some of those that none of the above solutions work (and that Xcode build path setting was already set to Unique), I've found a solution.
The framework must be built with a scheme for an iOS simulator device (any in the list) and NOT a Generic iOS Device, as Playgrounds do not support it. This one worked for me :
Here's how I proceed for OS X with Xcode 7.1:
Create new project: OS X Cocoa Framework, Swift. For this example I named it "TestPlaygroundFMK".
Create a new Swift file.
Add a class to the file. The code has to be public. I made this for our example:
import Foundation
public class Talk {
public class func sayHello() {
print("Hello from framework!")
}
}
Build the project now.
In the menu bar, click on File > Save As Workspace.
Create a new Playground and save it inside the project folder next to the Swift files.
The Playground must not have the same name as the project.
Build the project again.
Import your framework in the Playground and use it:
import TestPlaygroundFMK
Talk.sayHello()
I had my playground working fine but found that when I quit Xcode and re-opened it, I would get an error like this:
error:
/var/folders/ft/bmk8wh6s5ms4my2pxhn3qbp40000gn/T/playground1-9216e0..swift:3:8:
error: no such module 'Playground_Sources' import Playground_Sources
... where "Playground" is the name of my playground file.
The solution to this (in Xcode 9.0) is to:
click any playground file in your workspace
show the Utilities panel (opt-cmd-0)
under "Playground Settings" change the "Platform" to anything else then set it back.
In my case, it's set to macOS. I changed it to iOS then back to macOS (no recompilation necessary) and it started to work fine... that is until I close Xcode and open it again.
This is the only solution I found to work with macOS playgrounds. This bug still exists in Xcode 9.3.
I'm not sure i had the exact problem as the original question - but given the mix and age of solutions and the lack of this one that DID work for me, i thought I'd share.
In Xcode 11+ (tested this on 12.2), i'd often have Playgrounds fail to run givn the "No Such Module" error when it included a framework that is a part of the workspace.
The solution for me was in the "File Inspector" for the Playground, and the "Build Active Scheme" option:
This ensures the project was being built appropriately for the target, and now my annoying compile issues are gone!
Create a workspace as below:
Choose File > New > Workspace
Enter the workspace name, and specify its location in your file system
Click Save
Now, Create a Cocoa framework with swift file which have your class
Choose File > New > Project
Select Cocoa framework > Next
Enter Product Name and Language > Next
Before clicking create, in 'Add to:' and 'Group' list choose 'Your workspace name' as shown
Framework is added to your workspace
Create swift file in your framework
In Xcode right click on the folder where you want to add swift file and select new file...
Select swift > Next > Name your file > Create
Select your framework in Xcode -> Build your framework
Create playground -> Playground should also be part of your workspace, if not drag it to your workspace, it'll be added automatically.
Right click in Xcode's files section > New file... > Playground
import
Call your method
Note: If you just wanted to add classes instead of framework to your playground check the link How to import my own class into an Xcode 7 playground?
Solution that worked for me:
Setup your workspace and project/framework.
Delete derived data (How to remove derived data?)
DO NOT BUILD YOUR PROJECT YET
Create playground file with different name then your project/target/framework name under folder with either Swift files or where .xcodeproj is located (does not matter which one you chose)
Build your project under required platform (if playground is for macOS, then build project with macOS target, etc.)
When build finishes try adding some code to your playground and run it.
P.S. you might need to restart Xcode before 6th step.
The solution, from Apple Support, was to adjust my Xcode Preferences. On Locations :: Advanced my configuration was 'Legacy'. By changing to 'Unique', and undoing any paths I'd attempted to insert, Playgrounds can now import frameworks.
I don't think it's a stretch to say that Xcode is somewhat of a blackbox. I make no claim that this will work for anyone else, but for me, after having the same issue after creating a new playground and a new framework target, and all the other proposed solutions, what finally worked was deleting the workspace file and (in my case) regenerating it with cocoapods.
I have no idea why this worked.
Build the framework with release ,this worked for me. (Xcode 10.1, swift 4.2)
Insert a var at the first line and re run
var str = ""
I love the "Organize Imports" command in Eclipse to implicitly add and remove classes imported into a source file (as in Java or ActionScript).
Is there a command in Xcode to update the #import directives at the top of.m Objective-C files based on the classes referenced within the file?
You can do this by creating an Automator action and use that in Xcode as well as everywhere in Mac OS X. To do that, do the following:
Start Automator -> New
Choose "Quick Action" (or "Service" on older MacOS/Automator versions)
add a "run shell script" action
use sort | uniq as the script and check the "output replaces selected text" checkbox
save and give it a name (e.g. "sort & unique")
check "Output replaces selected text"
After you saved it, you can just select your imports in Xcode, right click and choose your "sort & unique" action to organize your imports.
This is not as good as the organize import actions in Eclipse or IntelliJ, because it doesn't removes unused stuff etc. but it's better than nothing.
PS: Got that from WWDC 2012 Sessions - Session 402 Working Effeciently with Xcode (starting at 6:15)
EDIT
I started using AppCode, the Objective-C IDE from JetBrains, and it has features like "organise imports". You should check it out: http://www.jetbrains.com/objc/.
I'm resolving this as not currently a feature of Xcode :(
I've found Cedar Shortcuts to at least be usable. It's not as good as Eclipse's import feature (it doesn't organize imports), but it can keep you from having to go to the top of a class file and typing an import statement. With this plugin you just place the cursor on the class that needs imported and press Ctrl + Option + I. Here's the github project. https://github.com/cppforlife/CedarShortcuts
I made a small Xcode plugin to sort the headers and remove duplicates of the file with a shortcut, you can check it out! - https://github.com/insanoid/CleanHeaders-Xcode
AppCode from Jetbrains can do this.
Yup. Like in Eclipse, this would be an awesome feature since developing in XCode (Cocoa Touch) does require quite a bit of class imports which are not easy to remember and Android development in Eclipse sorts this out with a simple keystroke combination that saves so much time ! Hope there is a way to do this in XCode soon !
WordService (freeware) from Devon Technologies, provides a service that can be used in any Cocoa app that can (amongst others) sort lines.
Alternatively, you can use an Xcode Extension, such as Imp
Swiftlint has an opt-in rule which if opted, will automatically sort the imports alphabetically.
Add - sorted_imports to your .swiftlint.yml file under opt_in_rules.
Run swiftlint autocorrect terminal command on project root directory (same where swiftlint.yml is stored).