Including a Playground in my project - ios

I'm working in Xcode 9 on a Swift project, and I'm in table flipping mode with this.
Here's my end goal: put my tests next to my logic in a playground so that as I write the logic, I can see the tests pass in close to real time.
I will also accept how to put my tests in a playground by themselves, I just don't want to be running tests on a simulator for every little thing.
My problems in trying to get this to work are many. First and foremost, I can't find a way to add a playground directly to my project/workspace. When I right click on a group in the project navigator and select 'New file...' I get this:
As you can see, no option for playground. Filtering also returns no results. If I press the plus button at the bottom of the project navigator, the same dialog (with no playground option) pops up.
I thought it's in another menu so I went to the File menu, and in "New >" I find an option for creating a playground. I select a blank playground from the chooser, choose where to save it (strange that it wanted to save it under 'Unsaved Xcode Documents') and a basic playground comes up in a new tab. Odd thing I notice, though, is that there's no longer a play button or scheme/simulator selector at the top on this tab.
It now doesn't recognize ANY modules other than Apple provided ones! And sure enough, creating the playground didn't actually add it to my workspace. I guess I have to manually add it to my project. Upon doing so, it will recognize my pods but none of my project files! Any class I try to use gives me an Unrecognized identifier. Trying to import my project doesn't work either, it just gives me a No module.
I tried clearing DerivedData, I tried cleaning, I tried restarting Xcode, I even made sure I had an iPad Pro in the list of simulators (since that seems to matter for some people).
In frustration, I deleted the playground, and resolved just to deal with running my tests constantly. But now, my project has persistent errors! Despite moving the playground to the trash, Swift continues to error saying 'Unresolved identifier' for that playground!

As the excellent Brandon Williams says in his talk at FrenchKit, a playground "does not get access to anything in the application target." Which is ridiculous, but hey, what are you gonna do. So the answer is: there is currently no way to do this the way I asked.
It IS possible, however, to achieve this functionality if your logic is in its own framework/cocoapod. That is, move the logic you want to test out of your app target and into an outside library that your app, subsequently, imports. This will allow your playground to import it as well, and you can test to your heart's content.

Related

Xcode 8.3.2 keeps on building after every keystroke

Whenever I try to change anything in my source files, the Xcode builds the entire project.
And since, I am in between writing the code, its obvious that there are errors (like "NSLog(#"ABC")") will throw an error if written halfway like this "NSL").
If I leave it idle, it fails for 5 to 10 times more and finally I see build succeeded. And again, when I try to change anything, it rebuilds on every keystroke and fails.
I have tried unchecking, "show live issues", "continue building after errors". Even a deep clean (command+shift+alt+k). After cleaning, its ok, but when I rebuild again, the same nut job building process continues.
This issue is not related to IBDesignables also. I have tried to uncheck "Automatically refresh views" in the storyboard and it did not work.
Any help?
Looks like, I can't open my storyboard as a different window, not even in a new tab. I used to have one of my windows for the source code and one for storyboard only. This made the switch between storyboard and source code much faster. But may be because of IBDesignables, its not possible anymore.
The problem is partially solved. Even if I open my storyboard now, it shows the same behavior of rebuilding again and again. But atleast, I can write my code in peace now with only source code files open in different tabs/windows.

Xcode: unit tests are misbehaving

I'm working on an app for iOS. I have 3 targets:
App — the app itself
AppTests — unit tests
AppUITests — UI tests
The third one is currently empty, but I intend to use it later. There are lots of tests in the second one.
Before yesterday, when I pressed Cmd+U, Xcode would happily run all tests for me (on a simulator, usually), and give me comprehensive report in the Debug area down below.
All of a sudden that changed. Now I see output from unit tests (AppTests) for less than a second, then it disappears, and a bit later the output from UI tests appears instead — which is completely useless, since there are no UI tests.
I'm not OK with this. UI tests sometimes print valuable information (not normally, but sometimes I use that). I need to see this output. And I kinda can: I see that it's still there in the log file, like DerivedData/App/Logs/Test/<long-guid-here>/Session-2016-06-16_18\:34\:44-EjeDwZ.log, along with lots and lots of other information that I don't understand. And there is another file, like Session-2016-06-16_18:34:44-Wlv6KP.log, in the same folder, with, as it seems, output from UI tests.
I want to see both, as it was before. I'm not OK with grepping through the giant log file.
What I tried:
Removing DerivedData folder entirely.
Cloning the same project from git into a new directory and starting it from there.
Reverting to an earlier version of the code, which worked fine before.
Removing ~/Library/Caches/com.apple.dt.*.
Trying tests on a different simulator.
Doesn't matter.
Another symptom is that the "Tests" panel in Xcode doesn't work anymore. It still correctly displays test suites and tests themselves, but small buttons to the right of tests, intended to run them, don't do anything. I still can run the suite by right-clicking on it and choosing Run AppTests (and I see the output in that case), but not by clicking on buttons. Also, I can run individual tests by clicking on the buttons in the code editor, to the left of test functions.
Does anybody have any idea how to fix it?
Xcode 7.3, Swift, if it matters.
Update:
After fiddling with "Edit Scheme" — specifically, changing "Debug" to "Release", running tests, and then changing back — "Run" buttons in the Tests Navigator suddenly started working. Issue with two different logs still remains, and I have no idea what to make of this.
Update 2:
"Run" buttons stopped working AGAIN, for no obvious reason.
Update 3:
OK, I figured out how to view tests output in the report instead of debug area.

Xcode stalls on "loading" when trying to create new project

Xcode 6 never gets past "Loading" when I try to create a new project. I actually was able to create a new project yesterday, but I decided to just ditch it completely (deleted). Clearly there must be a bad file somewhere, possibly related to the project I deleted, since I also see the same stalling behavior now with Xcode 5.1, which has worked fine for creating new projects in the past. Xcode 6 has worked fine when starting with an already existing project and still appears to do so. I did not see this exact problem in your data base, though one person saw the project creation freeze before reaching the stage I get to. Their solution was to delete Xcode and every possible file related to it, which I'm a little squeamish about, since I don't want to affect existing projects, and don't have an understanding of what the various files are for.
Deleting the DerivedData of Xcode should fix the issue. You can do this by heading to ~/Library/Developer/XCode/DerivedData (with a Finder window open press cmd+G) and deleting all the subfolders of it.
This is embarrassing, but if it could happen to me, maybe it could happen to someone else. The problem was that the window that came up after the second step in defining the type of project to be created, the one in which you choose where to save the project's files, extended off the bottom of the screen, so that only the very tops of the buttons were visible. Eventually, I realized the one to the far right might be the one to click to continue. Which it was. Feel free to delete this question.

Xcode going crazy! while coding, loses classes, references and doesn't autocomplete giving often <<error type>>

After I installed the latest version of xcode i'm having a very annoying issue.
While I'm coding, xcode goes nuts. Without me doing anything weird, just typing code, xcode stop recognizing classes.
For example: I want to add UITableViewDelegate to my class, but it doesnt recognize it. After i type it manually sometimes it recognize it and it is shown in purple, some other time it doesnt. But both times it won't really consider it, so if I try to write down a method of that delegate, it won't show it.
Other times if I try to call a variable of a certain class, while trying to autocompleting it, it shows <>.
Other times if I try to call any class, let's say I try to type var test = UIActionSheet, it just shows a few elements in the autocomplete list (raw types, primitives, the classes of my main project, but it doesn't show the majority of classes).
It's like it's missing the documentation and the link with the main frameworks...
I'm working on a simple tabbed app from yesterday and it's the 3rd time i started all over because of this issue, thinking that starting over would fix the issue, but it's not working.
If i open a different project while the issue is going on, the other project works ( but i recoded all over my app so it's not that one the issue, and i also have the same issue on other project... it just doesn't affect 2 project at the same time )
i tried deleting derived data
i tried restarting both xcode and the comp
What's going on?
Here are two screenshoots where you can see what's happening:
I had the same problem earlier.
Exit Xcode and delete Derived Data folder here ~/Library/Developer/Xcode
Restart Xcode and you should see the autocomplete working again.
You have to learn how to take it apart and put back together.
Either reinstall Xcode, if it doesn't help, create a new project.
Then copy source files one by one and see when it breaks.
Freshly install the XCode IDE version 6.1
Use CMD + F to find all _element.
Check that if you named one variable _element
The following code may reproduce the error:
class _element:NSObject {
}
let _element:UIImage = UIImage()
// type _element under this line

Today View Extension (Widget) not working

I found several other threads with similar problems, but no one has exactly the same problems.
Besides it DID work some time! the errors now keep occurring while it was working some time before..
When Running my app, that has a build Target "Today View Extension", I get no actual result.
The Extension is shown in Notification Center, but has no body (Simulator AND device).
Also when I try to run the App (not the target extension) and attach the process manually by PID I get this error:
I also had the error that my Extension (that has a "Bundle Display Name" entry in Info.plist for a custom Name) did show the Name of the Extension-containing App, and not the string that was set in the Info.plist
Strange thing is that sometimes it worked, sometimes it doesn't, but when it does not there were like five different reasons why not.
I want to ask people who have similar/same problems to post them here to collect all the issues appearing and possibly collect workarounds / solutions for these problems.
Thank you.
For anyone having troubles now:
With beta 4 and beta SDK 4 a lot of bugs were fixed:
[self setPrefferedContentSize:]
to set the views size is now working properly (if you have troubles viewing your Extension)
If your updated Extension is not showing in Notification Center be sure to have a look at the log output, there you can see what task the debugger is attached to, if there is "no Selection" try stopping and running again, it will work after some tries!
If you have questions feel free to ask,
Happy coding
I don't exactly have a solution, but I've observed this happening when anything is "wrong" with my Today extension. For example, if I don't have a file properly targeting the widget. I'm guessing that instead of just crashing to the home screen, iOS just gives you an empty widget? I've written about my own issues here, for reference.
I had similar issues. But it seems to be alright now. Since the "Today View" is an extension and is bundled with the containing app, you should just build and run the containing app. From there, you can pull down the "Today View" and if your widget / today view is not added, add it.
You should be able to see all your updated changes without a problem with this and you won't have to attach any process.

Resources