{"id":2931,"date":"2012-10-16T16:25:20","date_gmt":"2012-10-16T16:25:20","guid":{"rendered":"https:\/\/blog.jetbrains.com\/objc\/?p=1288"},"modified":"2019-06-05T11:43:32","modified_gmt":"2019-06-05T11:43:32","slug":"appcode-code-generation-101","status":"publish","type":"appcode","link":"https:\/\/blog.jetbrains.com\/fr\/appcode\/2012\/10\/appcode-code-generation-101","title":{"rendered":"AppCode Code Generation 101"},"content":{"rendered":"<p>When you\u2019re in the creative mood, developing a new app, or adding cool features to your current one, you definitely don\u2019t want anything to slow you down, right? What probably bugs the most is being stuck by typing each and every thing.<\/p>\n<p>Naturally, there is code completion, but on top of that, a lot of code constructs can be simply generated. AppCode comes with a wide set of code generation options, and it would take more than one post to cover them all, but I\u2019d like to make a quick overview of them so you\u2019d get the idea of the power behind it, and we\u2019ll get back to them in detail later.<\/p>\n<h3>Intentions<\/h3>\n<p>You know why intentional programming is such an awesome thing? I have a simple answer &#8211; because we all are lazy, some more, some less. I bet you know how tempting it is to start writing code using methods, classes, variables and whatever else, that doesn\u2019t exist yet. So why holding yourself back?<\/p>\n<p>AppCode lets you be as lazy as you want to. You know those lightbulbs in the gutter area that tell you right away when there\u2019s an error in your code? In fact not only they help you fix problems, but there\u2019s also a whole set of intention actions. Type a name of a method (or class) that doesn\u2019t exist yet and hit <code>\u2325\u23ce<\/code> to create it (or click the lightbulb in the gutter area).<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-5400\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/03\/objc-create_method_before@2x.png\" alt=\"\" width=\"600\" \/><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-1470\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/03\/objc-create_method@2x.png\" alt=\"\" width=\"600\" \/><\/p>\n<p>When you generate a method this way, and you want it to be declared in a private category, place the caret at method definition and hit <code>\u2325\u23ce<\/code>.<\/p>\n<p>That\u2019s not it! Say, you declare a property: hit <code>\u2325\u23ce<\/code> and AppCode will synthesize it.<br \/>\nAn instance variable needs to be moved to implementation? Need getter and setter for a property? Flip a binary operation? <code>\u2325\u23ce<\/code><\/p>\n<p>Localize a string? You know the shortcut, right? To see the whole list of available intention actions, just open <strong>Preferences | Intentions<\/strong>. Honestly, this shortcut is one of the most important ones, and I can\u2019t stress it enough.<\/p>\n<h3>Generate (\u2318N)<\/h3>\n<p>Another way to generate constructs is using <code>\u2318N<\/code> (or <strong>Code | Generate<\/strong> from the main menu). Here you can choose to create initWith and objectWith methods, declare and synthesize properties, declare members in the header file, etc. Most of these things are also available as intentions actions, which one to use is truly more the matter of taste and preference.<\/p>\n<p>By the way, if you invoke <code>\u2318N<\/code> from Project View, AppCode suggests you the different set of options: you can generate a new class, protocol, category or another file.<\/p>\n<p>Since two of the options are used much more often than others, and I&#8217;m talking about &#8220;implement\/override methods&#8221;, they have their own shortcuts to let you reach them even faster: <code>\u2318I<\/code> and <code>\u2318O<\/code>.<\/p>\n<p>1) <strong>Implement methods with <code>^I<\/code><\/strong><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1471\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/03\/objc-implement_objc_opt.gif\" alt=\"\" width=\"751\" height=\"320\" \/><\/p>\n<p>2) <strong>Override methods with <code>^O<\/code><\/strong><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1473\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/03\/objc-override_objc_opt.gif\" alt=\"\" width=\"630\" height=\"330\" \/><\/p>\n<h3>Surround With<\/h3>\n<p>Say, you have an expression or a set of statements you\u2019d like to put inside an <code>if<\/code> statements, what\u2019s the fastest way to do that? First, select what you want to be surrounded with <code>if<\/code> (by the way, when doing that, try using <code>\u2325\u2191<\/code>: with each hit it expands current selection to the next widest token, I\u2019m sure you\u2019re going to love this one). Now, back to our <code>if<\/code>. Just hit <code>\u2325\u2318T<\/code>, and choose <code>if<\/code>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-1475\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/03\/objc-surround_with@2x.gif\" alt=\"\" width=\"600\" \/><\/p>\n<p>Whenever you need an <code>if<\/code>, <code>for<\/code>, <code>@try<\/code>\/<code>@catch<\/code>,<code>while<\/code>, <code>(expr)<\/code>, <code>((Type)expr)<\/code> or something else &#8211; you can add it with <code>\u2325\u2318T<\/code>.<\/p>\n<h3>Live Templates<\/h3>\n<p>Live templates enhance Xcode code snippets by providing more functionality and easier management. Type <code>each<\/code> and hit <code>\u21e5<\/code> to iterate a collection, or <code>logm<\/code> and <code>\u21e5<\/code> to log current method.<\/p>\n<p>The whole list of live templates you can find under <strong>Preferences | Live Templates<\/strong>, and when you don&#8217;t remember the acronym for the construct you need, just press <code>\u2318J<\/code> to see the list of suggested templates.<\/p>\n<p>Under Preferences you can even add your own live templates, for example a new <strong>Surround with<\/strong> using <code>$SELECTION$<\/code> variable, and it will be available in surround with options when you hit <code>\u2325\u2318T<\/code>.<\/p>\n<p>That&#8217;s it for today, I hope you&#8217;ve learned something new. Stay tuned!<\/p>\n","protected":false},"author":70,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[601],"tags":[178,743],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/appcode\/2931"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/appcode"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/types\/appcode"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/users\/70"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/comments?post=2931"}],"version-history":[{"count":0,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/appcode\/2931\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media?parent=2931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/categories?post=2931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/tags?post=2931"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/cross-post-tag?post=2931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}