Can Swift playgrounds see other source files in the same project? - swift

I created the most simple custom class in a separate Swift file in my project:
class Foo
{
init()
{
println("I made a foo.")
}
}
Then, in a playground within the same project, I tried
var x = Foo()
Xcode didn't seem to like this, and told me that 'Foo' is an unresolved identifier. I'm somewhat confused about how playgrounds fit into the rest of the project structure, since any other Swift file in my project can resolve 'Foo' without issue.
How can I make my playground able to use custom classes I define in other Swift files in my project? I have tried naming the product module for the build target and importing that into the playground, with no success: the playground doesn't recognize the name of the product module.
Thanks in advance for the assistance. I know it is something simple.

There's two ways to use your project's code in a Playground
Playground's Sources Folder
Yes, in Xcode 6.3 Beta 3 (and hopefully, into the future):
Playgrounds are now represented within Xcode as a bundle with a disclosure triangle that reveals Resources and Sources folders when clicked. These folders contain additional content that is easily accessible from your playground’s main Swift code. To see these folders, choose View > Navigators > Show Project Navigator (or just hit Command-1).
Open up a new playground and hit cmd + 1 to see the left pane, then drag files into the source folder to use within the playground.
Note:
The files in the source folder are compiled to a framework which means if you want classes, functions, etc. to be accessible in the playground, they must be explicitly marked as public.
public class VisibleClass {
}
class InvisibleClass {
}
Source: release blog
Compile Project Into Framework
Move project to workspace if it isn't already. (File -> Save as Workspace) will do the trick
Add framework target to your project
Build framework
Make sure files you want to access are added to your framework target
Add Playground to workspace (NOT the project)
Use #testable import YourFrameworkName
Access code in playground
I made a write up here that goes into a bit more detail if you want to check it out.

They cannot. Playgrounds are self-contained. This will hopefully change in the future.
Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.

Yes.
I started by just adding a class file in the Sources directory.
I made everything public:
class
init
members
After much trying, nothing worked. The XCode crashed and after reopening it all worked like a charm.

In Xcode 10's Project Navigator:
Add the source code file to the playground's Sources folder.
Drag the file from the playground's Sources folder to the desired location in the project (you should see the little "plus in a circle" icon appear.
End the drag and then in the Add File dialog uncheck "copy if needed"
The source file now "lives" in the playground package; the Project refers to it (you can verify that with the File Inspector).
I tried it the other way around: file lives in project folder with reference in playground's Sources folder but it didn't work; I ended up with two copies of the source code file.

Related

In a Swift project, how to reference code that is a couple folders down to be in scope?

I am trying to structure my Swift project to be organized by scene.
I plan to have a folder for each scene, immediately within that folder the scene delegate, and within subfolders the views and models. Something like this:
Project
* Scene1Folder
- Scene1Delegate.swift
* Scene1Views
- Scene1View1.swift
However, this structure appears to make the Scene1View1.swift file out-of-scope of the Scene1Delegate.swift file.
Here is a screenshot of xcode showing the folder-structure of this part of my project, the Scene-folder and its sub-contents:
After moving my view folder from the project-root and into LookupScene, my SceneDelegate.swift file can no longer reference my ContentView.swift class. It shows this not-in-scope error:
How can I reference code within a sub-folder so that I can organize my project the way I want to?
UPDATE: This appears to compile despite the out-of-scope error shown by xcode. Appears that swift can find the reference even though xcode thinks it won't be able to.
The arrangement of files and "folders" (actually, groups) in the Project Navigator is merely a convenience to you. A "folder" does not constitute any kind of scope. Every Swift file, no matter where it is in the Project Navigator, is visible to every other Swift file, provided they are all part of the same app target.

Can't seem to import a framework into my current project in Xcode 10.2.1

Here's what I've done so far:
Create a Single View App project called MyApp
Create a Cocoa Touch Framework project called MyFramework
Created a Swift file with a class as public class FrameworkClass
Dragged MyFramework.xcodeproj into MyApp's Project Navigator - This caused Xcode to create a workspace.
I added the framework to MyApp.General.EmbeddedBinaries
Now it looks like this so far but I can't seem to import my framework still:
Please follow the steps to resolve your problem:
Choose your project, rather than a specific target, and open the
Build Settings tab.
In the Other Linker Flags section, add -ObjC.
If these settings are not visible, change the filter in the Build Settings bar from Basic to All.
Right before alt-tabbing Xcode to submit this response, I built on reflex and it turns out that it won't detect the references until you build once. After that, it should start to be detected by the text editor.
I was going to delete this question however, I already typed it up anyway so might as well. Hope this helps someone!

Classes in Swift files inside folder references not seen by Xcode 10's compiler

We have a project with nearly 1K Swift files. It just works well if everything is inside groups, but when trying to add some folder references to directories that contain Swift files, the compiler just can't see any thing defined inside these files (classes, for example). It works well for bundle and data files, but not for source code.
I also tried changing existing groups to folders using different methods (dragging, using the menu and manually browsing, etc.) and XCode stops seeing the entities declared inside these Swift folders' files. When I switch the same folder back to a group (delete -> remove reference -> add files to project...), it works again.
Please note that I opened the File Inspector, and already ensured that the target for the reference folder was checked. Also tried to uncheck it and checked again, just in case there is just-another-XCode-bug when adding files. Nothing worked.
Another experiment that I've done was consisted of moving a folder to the root directory, so the folder was not inside a group. Didn't work either.
After each try, I always cleaned the project's build folder, just in case.
Please note that the following questions did not provide anything useful tip for fixing this:
Xcode added folder in blue and related there files are not compiling
Moving Files into a Real Folder in Xcode
And of course, this another one does not make sense to me since I want folders, not groups, because I find them much easier to use and specially, to maintain, given the large amount of Swift files of this particular project: Adding Folder to Xcode Project is not Properly added
It works well for bundle and data files, but not for source code.
Yup, well, that's because those are completely different kinds of thing, and need to be treated in completely different ways.
You can't put code inside a folder reference (blue). That would simply mean "copy this code file into the body of the app", which would be silly. That's the kind of thing you would do with bundle and data files.
If the problem is to organize your code file references within the project window, you can use:
a group (yellow with a red mark in the lower corner)
or, a folder-linked group (yellow plain and simple)
In the latter case, the code file itself will also be placed in the corresponding folder on disk inside the project window, so this is also a mode of organization on disk.
NOTE But note that you must always let Xcode itself organize the contents of the project folder! You must work entirely within the Project inspector in the project window. Stay out of the Finder. Otherwise, you'll break your project.

Cannot add Alamofire to Swift Project

Trying to add alamofire to swift project using unstruction from here
Did all these steps, clean project a lot of timer and restarted XCode, nothing helps. Error does not dissappear
http://i.stack.imgur.com/kNU3R.png
"Cannot load underlying module for 'Alamofire'" and nothing to do
Changes I did:
1) added project file to my project
i.stack.imgur.com/eUe8E.png
2) added to build phases panel
i.stack.imgur.com/1wanl.png
3) added to general tab
i.stack.imgur.com/L3TTR.png
What`s wrong with that?
I had the same problem. I was able to fix it by doing the following.
Download the Alamofire "Source" folder.
https://github.com/Alamofire/Alamofire/tree/master/Source
Drag it into your xCode Project, and click "Copy Items if Needed". If you view the project in Finder, the Alamofire Source folder should be in the project itself. (Ex: I have 3 items. Example App, Example App.xcodeproj, Example App Tests. The folder should be in Example App.)
Finally, you should be done! However, now, you do not need to import Alamofire, since you are not actually loading a framework.
For example, instead of doing Alamofire.upload, you will now only do upload.

Rename a class in Xcode: Refactor... is grayed out (disabled). Why?

Why is Refactor... grayed out (disabled) in Xcode?
I'd like to rename a class.
Select the class's symbol in its header file - i.e. the bit just after #interface. Then the refactoring stuff should be enabled.
If you are using Objective-C++ (i.e. mix Objective-C with C++ code) then refactoring is disabled in xcode since it does not support refactoring of C++ code.
Refactor might also be disabled if affected files (most likely the file with your class in it) are not saved.
I've been using Xcode for 5 years now, and refactoring has never worked correctly (even xcode 4.6 has major bugs where it WILL corrupt your source code!).
The workaround has always been (still works 100%, even in cases where Apple's code fails)
use shift-command-f to find all uses of the file
select "replace" in the search settings
"replace-all"
do the following for the .h file, and REPEAT IT FOR THE .m FILE (if you have one):
right click the original file, and select "show in finder"
delete the file from xcode (select "delete references only" when asked)
rename the .h (and .m if you have one) in Finder
in Xcode, select "Product -> (hold down Alt) -> Clean Build Folder"
quit xcode (you can usually get away with not doing this - but NOTE: there are some other MAJOR bugs in Xcode where it crashes itself if you don't do this)
re-open xcode
drag/drop the .h and .m back into Xcode
wait a few seconds (some of Xcode's core methods are asynchronous - allowing it to corrupt your project)
finally, when it seems to be doing nothing (and your hard disk isn't making any noise any more): cmd-b to re-build
I have a 100% success rate with this method. I just tried refactoring with Apple's "Refactor -> Rename" in latest xcode and it failed - again!
(this time with the incorrect error: "Unable to determine the language of", one of those error messages where Apple put the wrong text in place)
I'm going to my project at finder, then change files name by get info.
After that, at xcode -> Project Navigator I delete the files.
At end, I click right on the class and Add files to ..., and add these files again.
It worked for me.
For me I realized Refactor was disabled because the Xcode project I had opened was referencing a Base SDK that was missing. Edit Project Settings and in the Build tab set the Base SDK to one that you have (like for me this was iOS 4.2). This enabled Refactor for me.
Also, it could happen that you renamed the filename for the class, either outside xcode or by ctrl-clicking the filename and then renaming it.
xcode refuses to refactor if filename does not match with the class name.
Go to your class' header file and find the line that looks similar to this:
#interface YourClassName
Right click on the class symbol (e.g. YourClassName) and you should be able to select Refactor -> Rename...
I just tried this and it works in Xcode 5.
This may be a bit late, but I stumbled across this post because I was unable to refactor my "ViewController.swift" file to "WhateverViewController.swift". I tried selecting the file in the Project Navigator and then selecting "Editor -> Refactor" from the top menu, but 'rename' is always greyed out.
Instead, what worked was selecting the ViewController name from the editor. So if you have:
class ViewController: UIViewController {
// Code here...
}
Highlight the "ViewController" word and then select Refactor from the menu or right-click and select Refactor -> rename.
Hopefully that helps...
Had this problem as well. I ran through trying to find missing SDKs, saving files, and looking for Objective-C++ code as mentioned above, and all it took to fix my machine was rebooting XCode.
Seems a little buggy still.
BTW, this was for XCode 4.0.1
Refactoring works If you first change the file name in the project navigator.