Efficient use of your Xcode

Source: Internet
Author: User

(Via:vonglo's Dev Space original: supercharging Your Xcode efficiency)

In Hollywood movies, hackers often see their fingers jumping on the keyboard, and the code on the terminal is flying. If you think about that in the movie, then you've come to the right place. This tutorial will teach you to be more like a programmer in Xcode programming, which you can call magic, crazy technology, good luck, or hacking. Without a doubt, after completing this tutorial, whatever you think, you will feel cool, and use xcode more efficiently, even with these new discoveries to keep your code from destroying. The contents of this chapter include: Start Daily Xcode Tasks shortcut Keys the behavior of Xcode Small Quiz Acceleration make the designer happy Ascension and Transcendence Code Snippets whither 1. StartTo look cool, there are some essential factors. The following list is a subset of them: (1) Perform tasks quickly (2) precise (3) simple and beautiful code to be more cool, you can try to complete each task in the tutorial without a mouse or touchpad. The learning journey begins with learning some useful Xcode features and then training by fixing some bugs in cardtilt. Finally, you'll tidy up your code to make the entire interface look neater. As you learn this tutorial, keep in mind that it doesn't teach you to do an app, but rather teaches you to use Xcode more effectively than ever to develop apps. This tutorial is based on a basic understanding of Xcode and then focuses on improving your productivity. Everyone has their own programming habits, this tutorial does not force you to form a certain style. Throughout this tutorial you will see alternatives to some of the commands, as you learn in this tutorial, concentrate on refining and forming your own programming style, and don't let small errors affect you. Note: If you are not familiar with Xcode, it is recommended that you learn the following two tutorials first. Tutorial 1 and Tutorial 2 download Cardtilt-starter and then you can write the code happily. 2. Daily Xcode TasksThere are a number of techniques you use frequently in Xcode, which gives you a closer touch with these tips, and then explains some of the tricks you should take to deal with them. In this process, you will find new ways to use them based on these tips. These tips will be the Ninja star on your programming tool belt. In Xcode before opening the download Good Project cardtilt, do not hurry to see the code, First will you Xcode window and to one by one corresponds is not found your window and figure does not correspond, first don't worry. In the shortcut keys section below, you will learn how to easily display and hide these corresponding windows. Here is a quick tour of each of the separate areas that make up the window: The Toolbar (toolbar):You choose the view, run the app, and switch places in different layout interfaces The Navigation area (NAV):Navigate your entire project, warning, error, etc place The Editing Area (edit zone):Where all the miracles were born, including the jump bar above it The Utility Area (tool zone):Contains detectors and some libraries The debugging area (Commissioning zone):All of the view areas listed above, including the debug window and the variable detector, are necessary parts of Xcode, which you may need to use during your development process, and usually do not need to show them at once during development, and some shortcuts will be shown to show/hide the view areas quickly. 3. Shortcut keysIn this section, you will first learn how to master these shortcuts, thanks to some patterns, the most efficient shortcuts are very easy to remember. The first thing you need to know is the relationship between the various areas of Xcode and the modifier keys, here's a quick tour: some things to navigate, control the right side of the navigation area, such as Assistant Editor, Utility editor Control:Some interaction of the jump bar on the edit area the second need to know the relationship between the number keys and the tab bar, the number keys and the modifier key combinations mentioned above can be switched back and forth between the tabs. Usually the number keys correspond to the index position of the tab page (starting at 1), and 0 is usually used to show/hide the area. Can it be more intuitive? The following are the most common key combinations: command 1~ 8: Jump to different positions in the Navigation Area Command 0: Show/Hide Navigation command alt 1~ 6: Jump command alt 0 between different detectors: Show/close Tool area. Control Command Alt 1~4: Jump between different libraries to control 1~ 6: Jumps on different tabs of the jump bar. The last and simplest is the return key, which, when combined with command, allows you to switch back and forth between different editors in Xcode. Command + ENTER: Display standard Single Window editor command ALT Enter: You can guess what it does, its function is to open assistant Editorcommand Alt Shift Enter: Opening the versioning editor is also important to show /Hide Debug Area shortcut key is Command + Shift + Y, to remember this you can use this phrase to memorize "Y is my code not working?" (Translator Note: Y homophonic why). If you forget some shortcut keys, you can find most of the shortcuts in the Navigate menu bar of Xcode. At the end of this part of the study, you will be surprised to find that you just use the keyboard to make Xcode the various changes. Behavior of 4.XcodeUsing shortcut keys to manage Xcode's interface is a great thing, have you ever thought about something better? For example, let Xcode automatically switch to the interface you want to see. And then we'll learn something cooler. Fortunately, the behaviors (behavior) provided by Xcode allows you to easily implement the above things. They are a set of well-defined actions that have a specified event trigger, such as build a project. The range of actions here ranges from changing the interface to running a custom script file. To take a look at an example, quickly modify the downloaded project's CTAPPDELEGATE.M file so that its runtime generates the debug window output, replacing the Didfinishlaunchingwithoptions method with the following method
    1. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
    2. {
    3. [[UIApplication sharedapplication] setstatusbarhidden:yes];
    4. //Override point for customization after application launch.
    5. NSLog (@"Show me some output!");
    6. return YES;
    7. }
Run the program, and then look closely at the debug area, and you will see that the debug area appears as the app runs, as follows: To see what defines the above event, in Xcode->behaviors->edit Behaviors open Behavior preferences, On the left you will see the collection of all events, and on the right are some of the column actions that the event can trigger. Click generates output under the Running bar and discover that it is set to display the debug area. 4.1 Some recommended behaviors Note:Depending on your development environment, I recommend two different ' generates output ' events to trigger the action set. If you have multiple screens, try using the first one, and if it's a single monitor, try to get to the second method. 4.1.1 Method One if you develop on two or more screens, putting the debug zone on the second screen is not more convenient, you can set up the following so now, run the program, and then you will see a separate window appears, put it on your second screen, is not much more efficient? 4.1.2 Method Two if it is single, by hiding the tool board and setting the Output window to occupy the entire debug area to maximize the effective area of the Output window, set the following and then run the program, and then look at Xcode, see if it is executed according to your command. When the program is paused, you may also want to change the behavior of Xcode, to the pauses event under the Running bar, and then change its settings to the following: Now whenever you set a breakpoint, you will get a new display variable named fix and a tab for the Output window, Then automatically navigates to the first issue. The last behavior you're about to create is my personal favorite, it's a custom behavior-set a shortcut key. When triggered, it transforms Xcode into the layout I specified for the next development, called Dev mode. We can click "+" in the lower left corner of behavior preferences, then name it dev mode, double-click the symbol to the right of dev mode and enter Command. To define a shortcut key. Next, set the action for the event: now, as soon as you press command. It triggers the action set above, which means that an identical clean development interface appears. The following describes the Xcode tab name, which is perfect for using the behavior together. Note:Xcode tab Names (Xcode sign): You can modify the name of a tab by double-clicking its title, which is a useless feature for itself, but it becomes very powerful when combined with behavior. In the second example above, when changing the pauses behavior, a tab called fix is named, which means that when the behavior is triggered, Xcode will use the Fix tab (if it exists) and if it does not, it will create a new tab named fix. Another example is the multi-screen starts behavior, if a tab named Debug is opened before it is run, it will reuse the tab instead of recreating one. In this way, you can create very interesting behaviors. So far, we can take some time to review what we have just done, and don't worry, this tutorial will be waiting for you. 5. QuizIn the following sections, you will use the techniques learned above in the quiz, and then learn some new tricks in the Cardtilt project. Running the Cardtilt project, you see the following screen which is not the picture you expect to see? It's time to get rid of these bugs! 5.1 Locking Bugapp appears to be having problems loading data, and your task is to destroy them. Open the Ctmainviewcontroller.m file and then enter Dev mode, the shortcut key for the command previously set. Note the first few lines in the Viewdidload method
    1. Self.datasource = [[Cttableviewdatasource alloc] init];
    2. Self.view.dataSource = Self.datasource;
    3. Self.view.delegate = self;
It looks like Cttableviewdatasource implemented the Uitableviewdatasource protocol, providing data for tableview. It's time to show your Xcode technology, hold down the command key, and click Cttableviewdatasource to open CTTableViewDataSource.h in the editor. CTTABLEVIEWDATASOURCE.M should already be in your Assistant editor, if that's not the case, open the top jump Bar and switch to Assistant Editor's counterparts mode like one. Looking through the code, you will find the array members loading the data, the LoadData method loads the data from the bundle to the array members. Right-click anywhere on the right-hand side of the assistant editor, select Open in Primary Editor, the CTTABLEVIEWDATASOURCE.M file is opened in Primary Editor, and the following animation is the display of this step: Note:  To look cool, you can do everything above without a mouse, as follows:  1. Also press COMMAND + Shift + O, enter CTMAINVIEWCONTROLLER.M in the input box, and return to open the file. 2.Command +. Go to enter dev mode. 3. Place the mouse over the Cttableviewdatasource and then press COMMAND + Control + J to jump to the cttableviewdatasource definition. 4. Press COMMAND + J, and then press ENTER to convert the focus of Assistant editor 5. Press CTRL + 4来 to pull the jump Bar, then use the arrow keys and the ENTER key to select COUNTERPARTS6. Hold Command + ALT, open the CTTABLEVIEWDATASOURCE.M file in Primary Editor 7. The result step above does not look efficient, but it makes you look cool.  5.2 fix bug in self.members = json[@ "Team"]; Make a breakpoint to determine whether the data is loaded into the array members, and then run the program.   If you are not familiar with basic settings breakpoints and debugging, you can look at this tutorial first.   Just as you saw in the Xcode behavior before, generates output will be triggered first, immediately after the pause behavior. Because you had a custom setting in Pause, Xcode creates a new tab called Fix, which is designed for debugging. Switch your gaze to the variable detector variable inspector, you will find that the array members are nil, in the LoadData method array members are populated, the code is as follows  
    1. Nsdictionary *json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error];
    2. Self.members = json[@"Team"];
Looking at the JSON in the variable detector to see if it is loaded correctly, you will find that the first value in the data is @ "Rwteam" instead of @ "Team", and when the array members are loaded, the value is wrong and a bug is found. Let's fix the bug by following the steps below: (1) Use Command +. To enter development mode (2) Press COMMAND + Option + J to jump to filter bar and then enter Teammember (3) and press ALT while clicking Teammembers.  JSON in Assistant editor to open it (4) Finally, using "Team" to replace "Rwteam" is an animation of the four steps above now remove the breakpoint, and then run the program, it should look like this. It looks better than before, but there are some bugs, such as Ray and Brian's title description bar missing. Next, we'll fix these bugs to learn more cool tricks. 6. AcceleratingYou should already know Uitableviewcells in Tableview:cellforrowatindexpath: method is loaded, so use Open Quickly to navigate to the method, and then follow the steps below: (1) Press COMMAND + Shift + O to Exhale open Quickly (2) input Cellforrow (3) press ENTER to open it command and then click Setupwithdictionary to jump to its definition, then you will find some code to load the description
    1. NSString *abouttext = dictionary[@"about"]; //Should this is aboot?
    2. Self.aboutLabel.text = [Abouttext stringbyreplacingoccurrencesofstring:@"\\n" withstring:@"\ n"];
It uses the data in dictionary[@ "about" to load the label. Next exhale the open Quickly, then open Teammembers.json, and use ALT + command to open it in Assistant Editor. Check the about value, and then you'll find someone who spelled the about aboot, we can use the global find and Replace to fix the bug. Of course you can do this directly in the file, but using find navigate looks cooler. Open Find navigate, then switch to replace mode via the top jump bar, enter aboot, and then enter. In addition to the Teammembers.json file there is another place to use the aboot, don't worry, check the CTCARDCELL.M search results, and then press Delete, move the mouse to replace filed, and then enter about, click Replace All, everything is done. The whole process looks something like this: Note:Advanced Tip: Use COMMAND + SHIFT + Option + F to open the Replace mode of Find Navigator, if you have trouble, you can also use COMMAND + SHIFT + F to open Find Navigator's lookup mode, if You still have trouble, use COMMAND + to open Find Navigator, and then turn on Find or replace mode and choose the one that suits you best. Run the program and it looks like this 7. Make the designer happyToday's commissioning task is over, give yourself a little applause, and start running the program. Before you can show it to someone else, you need to make sure that the app's interface is perfect. Especially when this person is a very serious designer, this section teaches you some tips on interface builder that will make you more cool. Open Mainstoryboard.storyboard, usually you want to open Interface Builder while opening the standard editor and tool area (utilities areas), so we can customize a new called IB Mode behavior, when you look at the following custom release, try to create one yourself and then customize it without having to be exactly the same. I set the shortcut key for this mode to command Option. Now you will see a comfortable Interface Builder interface to see Ctcardcell. First you want MainView to be in the middle of Contentview. There are two techniques to do this: Control + SHIFT, and then left-click anywhere in MainView, and you'll see a pop-up view that lets you select all the views under the cursor, as shown in: This method makes it easy for you to select MainView, although cardbg Cover it up. With MainView selected, hold down ALT and then move the mouse around the edge of Contentview to see the spacing between them. Here's an animation of the process: it's not so elegant, it doesn't look cool. To solve this, we can adjust the size of the view. Click Editor->canvas->live autoresizing to force the child view to resize when the parent view size changes. Hold ALT while dragging the corner of the Mainview until the distance is 15 per side. Note:Drag-and-drop is a pixel-level technique, and you can try different ways of adjusting it, just like the animation below. In many cases, it is more likely to use ' Size Inspector ' to adjust the complex layout rather than dragging with the mouse. Try using the same techniques to align Titlelabel, Locationlabel, Aboutlabel, and keep them in the vertical direction of the interval of 0. Hold down ALT to see the interval between three labels when the mouse moves. Have you found that the left edge of the three labels is not aligned? The designer would like to make Namelabel and Weblabel left-aligned. With vertical Guide We can easily accomplish this task. Select Cardbg, and then click Editor->add Vertical Guide in the menu bar, the shortcut key for this step is command-|,horizontal guide, which is the most visible of the shortcut keys. When you have vertical guide on the view, drag it 10 points from the left edge of the cardbg. Then all the views will be close to vertical guide, very neat, and continue to work on the other label. Note:xcode is not so perfect, sometimes when you create guideline, there are some problems, you can open other files and then cut back to Storyboard page, and then storyboard will be reloaded, this time the problem is usually automatically resolved. Cool Advice for you: All views are best to fit on guideline, guideline not necessarily at the same level. is a replay of the alignment process: I bet you can't wait to show your designer what you're doing right now! 8. Ascension and TranscendenceThe steps above allow you to get a useful app and make your designers feel satisfied, so the next thing to do is make the code more concise. Quick Open (Command + Shift + O) ctcardcell.m, remember to enter Dev Mode, you should not forget these shortcuts. Look at the list of CTCARDCELL.M top messy properties
  1. @property (Weak, nonatomic) Iboutlet UILabel *locationlabel;
  2. @property (Strong, nonatomic) NSString *website;
  3. @property (Weak, nonatomic) Iboutlet UIButton *fbbutton;
  4. @property (Weak, nonatomic) Iboutlet Uiimageview *fbimage;
  5. @property (Strong, nonatomic) NSString *twitter;
  6. @property (Weak, nonatomic) Iboutlet UIButton *twbutton;
  7. @property (Weak, nonatomic) Iboutlet UILabel *weblabel;
  8. @property (Weak, nonatomic) Iboutlet Uiimageview *profilephoto;
  9. @property (Strong, nonatomic) NSString *facebook;
  10. @property (Weak, nonatomic) Iboutlet Uiimageview *twimage;
  11. @property (Weak, nonatomic) Iboutlet UILabel *aboutlabel;
  12. @property (Weak, nonatomic) Iboutlet UIButton *webbutton;
  13. @property (Weak, nonatomic) Iboutlet UILabel *namelabel;
  14. @property (Weak, nonatomic) Iboutlet UILabel *titlelabel;
In this section, you will create a custom service to execute script commands to implement the property's quick and orderly arrangement. Note: If you are unfamiliar with these script commands, they are quite well understood. Sort is in alphabetical order, ' Uniq ' is the number of rows that will delete all duplicates. ' Uniq ' may not come in handy here, but it's great to manage ' #import '. Mac OS x allows you to create a global generic service for the entire operating system, which you will use to create a scripting service in Xcode, set up as follows: (1) Use Spotlight to search for Automator and open it (2) and then click File->new, and select Service (3) in the Actions filter bar, enter the shell, and then double-click Run shell script running shell scripts (4) on the menu bar of the newly added service, check the output replaces selected Text (5) switch the script content to sort | Uniq (6) Press command S at the same time to save the service as Sort & Uniq The final window looks like this: cut back to Xcode, then select the chaotic attribute area in CTCARDCELL.M, right click, select Services-So RT & Uniq, then observe how neat the code is after execution. You can look at one of the following: 9. Code SnippetsGoing here means that the basic debugging tasks that can make you look cool are over, and I want you to feel more cool next. You may want to learn more skills, and fortunately this is the last skill to share. You might have used Xcode's code Snippets feature before, and some of the common ones are for in and dispatch_after fragments. In this section, you'll learn how to create a custom code snippet that looks great when you reuse the code snippets. You will create a snippet of code that gets the singleton. If you are not familiar with the singleton pattern, you can take a look at this tutorial. The following may be a common code template that you use in singleton mode
    1. +  (instancetype) sharedobject { 
    2.   static id _sharedinstance = nil; 
    3.   < span class= "keyword" >static dispatch_once_t oncepredicate; 
    4.   dispatch_ Once (&oncepredicate, ^{ 
    5.     _sharedinstance =  [[self alloc] init]; 
    6.   });  
    7.   return _sharedinstance; 
The cool part is that this fragment also contains DISPATCH_ONCEP fragments. In engineering cardtilt Create a new class named Inherit from NSObject named Singletonobject, you don't need to do anything for it except as a place to drag snippets of code. Follow the steps below: (1) in the next line of the @implementation paste the above code into SINGLETONOBJECT.M (2) with the shortcut key command Option Control to open the Code snippet library, In the Code snippets Gallery you will see the default Code Snippets library contained in Xcode. (3) Select the entire +sharedobject method and drag it to the Code Snippets Library. It looks like this: the newly created snippet will be at the very bottom of the code snippet library, and you can drag it to any file you want to drag and let's try it. Double-click the code snippet you just created, and then click Edit. The pop-up view is very useful, in fact they are all important, so make a short explanation. (1) Title and Summary: Name and description of the snippet in the Code Snippet Library (2) Platform and Language: code snippet matching platform and programming language (3) completion Shortcut: Shortcut keys entered in Xcode (4) Completion Scopes: The scope of the code snippet, which is 10 excellent for keeping the code Snippet library neat. Populate the properties inside as follows: 9.1 TokensWhen you join a token, the snippet becomes very powerful, because it allows you to tag the code in the fragment without having to hardcode it. By using the TAB key, they are very easy to modify, just like auto-completion. In the fragment just enter < #TokenName #> can add a token, create a token using shared< #ObjectName #> override Sharedobject, it looks like this: Click Done to save the fragment, Then come and use it. Enter Singleton accessor in the singletonobject.m file, and then use auto-completion when it appears it is very useful to create a code snippet for frequently used code. 10. WhitherSummarize what you've learned in this tutorial: (1) Use shortcut keys to change the layout of Xcode (2) Use custom behavior to change the layout of Xcode (3) Use Assistant Editor (4) to quickly open a file in Xcode (5) in the Find Navigator Delete Search results (6) in interface Builder uses shortcut keys and guideline to align Views (7) Create a service to use in Xcode (8) Create and use a custom code snippet (9) The most important thing is that you know how to be a master of Xcode, it's simple, right? Show these cool tricks in front of your friends and your friends will be amazed and understand your excitement. There are a number of ways to improve your xcode efficiency, such as: using the Doxygen style comments using the Xcode plugin the next step is to find new cool tricks, I hope you can enjoy the whole tutorial learning process, if you have any questions, comments, or want to share the cool skills you know, Please indicate in the comments below.

Efficient use of your Xcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.