PJ’s path to iOS development

Introduction to the

When I used Xcode Playground for the development of WWDC 2019 Scholarship project, I originally thought that the Xcode 10.1 version designated by Apple would have a good optimization, but I didn’t expect that the effect was surprisingly same as that used two years ago!! Made me wonder, “Is this a tool made by Apple?” . From the beginning of knowing Xcode Playground to now, I have always felt that Xcode Playground is a magic tool. I have tried to use Xcode Playground when explaining relevant development knowledge points to students in the club, but I had to give up every time I tried.

Next, I’ll take a look at some of the problems I’ve encountered using Xcode Playground and some of the solutions.

How do you write that?

We create a Single View project as shown below:

Then you should see a neat project style:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView(a) {
        let view = UIView()
        view.backgroundColor = .white

        let label = UILabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello World!"
        label.textColor = .black
        
        view.addSubview(label)
        self.view = view
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController(a)Copy the code

OK, the code is very easy to understand, if you use is also the Xcode 10.1 in PlaygroundPage. Current. The liveView = MyViewController () on the left, will see a “play” button, click it to run.

But what about the interface after it runs? You can select the view display direction as indicated by the arrow below:

Everything is fine! This is the same as before, just set up the active view of the PlaygroundPage. We are confident that we will be able to play Xcode Playground well! In accordance with the previous engineering habits, the project began to be stratified and divided into modules.

Here’s the problem!

I am going to partition modules in Sources as folders, but I cannot create folders. Yes, you can’t create folders on Xode Playground directly, but you can create folders in Finder if you have to.

Well, since there’s no convenient scene folder, let’s forget about it. I guess Apple doesn’t want us to do big projects on Xcode Playground. Finally, I can start writing code!

So let’s write HomeViewController first. Based on the sample code you might write something like this:

class PJHomeViewController: UIViewController {
    override func loadView(a) {
        view.backgroundColor = .red
    }
}
Copy the code

Next use it on playground!

“Huh? Why is there no code prompt? Get used to it. “– that’s what you’re probably saying to yourself while you’re writing code on playground. With a simple class name, we can quickly write the following code:

  
import UIKit
import PlaygroundSupport

PlaygroundPage.current.liveView = PJHomeViewController(a)Copy the code

After waiting for a while…… (It could be a minute, it could be 10 seconds, it could be 3 seconds)

You receive a prompt: Use of Unresolved Identifier ‘PJHomeViewController’. PJHomeViewController does not exist!

“Impossible! It can’t be!” “And fell into deep disbelief.” Have I been writing iOS for so long that I can’t even create a class wrong?” . After going over the code, making sure that you wrote it yourself and that there was no problem, start searching the Internet for answers.

Finally, you’ve found the solution! The classes in Sources are not in the same Module as playground, so we need to expose the PJHomeViewController. You change the code to:

import UIKit

public class PJHomeViewController: UIViewController {
    public override func loadView(a) {
        view.backgroundColor = .red
    }
}
Copy the code

Then go back to playground and run! Huh? Why are you spinning around? Restart Xcode! Huh? Why not? LoadView does not set a frame for the view. The modified code is:

import UIKit

public class PJHomeViewController: UIViewController {
    public override func loadView(a) {
        view = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667))
        view.backgroundColor = .red
    }
}
Copy the code

Now run the project again, we finally see the red screen! Out of breath ~

Get ready to roll!

Now that we know the drill, we’re finally ready to hit the ground running!

In the middle of a big task, you’ll be asked, “What happened to the code prompt? !” “How come the error exception doesn’t go away?” “Why is it taking so long?” “Why hasn’t the directory been updated since the file was deleted?” And so on, so!

I highly recommend using Xcode as a priority for source code writing and debugging. Coding and debugging on Playground is a “very painful” experience, and if you have a poor device, the “live preview” effect of Xcode Playground is almost useless.

The assumption is that the implementation of Xcode Playground ran in a separate process to do differential compilation of files, which in some cases caused “freezes” or even the entire Xcode to break.

But that’s not the case on the iPad at all. Try using Swift Playground instead of Xcode Playground.

conclusion

Xcode Playground has great advantages, but at present, the disadvantages are gradually decreasing. Xcode Playground is very unfriendly to the old machine when I use the two machines of 2015 15-inch high matching MBP and 2017 15-inch high matching MBP, which makes it difficult for me to move. Finally give up and use Xcode to develop and then drop into Xcode Playground to see the effect.

In the next article, I will introduce the complete content of my WWDC 2019 Scholarship project.