{"id":1829,"date":"2015-06-15T10:22:40","date_gmt":"2015-06-15T08:22:40","guid":{"rendered":"http:\/\/www.flagsoft.com\/cmswp\/?p=1829"},"modified":"2016-10-20T13:08:30","modified_gmt":"2016-10-20T11:08:30","slug":"swift-how-to-input-a-number-from-cli-command-line-interface","status":"publish","type":"post","link":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/","title":{"rendered":"Swift: How to input a number from CLI Command Line Interface"},"content":{"rendered":"<p>Is there no input() function in Swift? No. Not for now. But how then can we enter a number or a string from the command line interface (CLI)?<\/p>\n<p>1. Copy this function to your code:<\/p>\n<pre>func input() -&gt; String {\r\n\u00a0\u00a0 \u00a0var keyboard = NSFileHandle.fileHandleWithStandardInput()\r\n\u00a0\u00a0 \u00a0var inputData = keyboard.availableData\r\n\u00a0\u00a0 \u00a0var strData = NSString(data: inputData, encoding: NSUTF8StringEncoding)!\r\n\u00a0\u00a0 \u00a0return strData.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())\r\n}<\/pre>\n<p>2. Use it like so:<\/p>\n<pre>print(\"Enter number: \")\r\nlet n_str = input()       \/\/ NOTE: n_str is an Optional (type), this means String or nothing\r\nlet n = n_str.toInt()!    \/\/ NOTE: ! \"unpacks\" the Optional type n_str<\/pre>\n<p>&nbsp;<\/p>\n<p>3. Testing: What happens, if I enter a Number:<\/p>\n<pre>Enter number: <strong>12<\/strong><\/pre>\n<p>Result: Just seems to work<\/p>\n<p>&nbsp;<\/p>\n<p>4. Testing: What happens if I just enter ENTER:<\/p>\n<pre>Enter number:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ &lt;--- I just hit <strong>ENTER<\/strong> here<\/pre>\n<pre>fatal error: unexpectedly found nil while unwrapping an Optional value\r\n0\u00a0 swift\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00000001039d82b8 llvm::sys::PrintStackTrace(__sFILE*) + 40\r\n1\u00a0 swift\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00000001039d8794 SignalHandler(int) + 452\r\n[...]\r\n<\/pre>\n<p>Result: It crashes, which is better then continue with undefined values<\/p>\n<p>5. Testing: What happens if I just enter a string not a number:<\/p>\n<pre>Enter Number: <strong>abc<\/strong><\/pre>\n<pre>fatal error: unexpectedly found nil while unwrapping an Optional value\r\n0\u00a0 swift\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x000000010bc512b8 llvm::sys::PrintStackTrace(__sFILE*) + 40\r\n1\u00a0 swift\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x000000010bc51794 SignalHandler(int) + 452\r\n[...]<\/pre>\n<p>Result: It does crash, witch is better then continue with undefined values!<\/p>\n<p>6. Conclusion<\/p>\n<p>So, sometimes it's a better your program crashes then just go ahead somehow undefined as it's possible with plain C for example.<\/p>\n<p>Missing Swift Computer Language Feature: But really, Swift should and has to include an input() function by default. Apple, if you read this, just do it \ud83d\ude09 Swift seems to be a great new computer language and I start to really like it.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is there no input() function in Swift? No. Not for now. But how then can we enter a number or a string from the command line interface (CLI)? 1. Copy this function to your code: func input() -&gt; String { \u00a0\u00a0 \u00a0var keyboard = NSFileHandle.fileHandleWithStandardInput() \u00a0\u00a0 \u00a0var inputData = keyboard.availableData \u00a0\u00a0 \u00a0var strData = NSString(data: &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\"> <span class=\"screen-reader-text\">Swift: How to input a number from CLI Command Line Interface<\/span> Weiterlesen &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[9],"tags":[],"class_list":["post-1829","post","type-post","status-publish","format-standard","hentry","category-swift"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift: How to input a number from CLI Command Line Interface - Flagsoft<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift: How to input a number from CLI Command Line Interface - Flagsoft\" \/>\n<meta property=\"og:description\" content=\"Is there no input() function in Swift? No. Not for now. But how then can we enter a number or a string from the command line interface (CLI)? 1. Copy this function to your code: func input() -&gt; String { \u00a0\u00a0 \u00a0var keyboard = NSFileHandle.fileHandleWithStandardInput() \u00a0\u00a0 \u00a0var inputData = keyboard.availableData \u00a0\u00a0 \u00a0var strData = NSString(data: &hellip; Swift: How to input a number from CLI Command Line Interface Weiterlesen &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\" \/>\n<meta property=\"og:site_name\" content=\"Flagsoft\" \/>\n<meta property=\"article:published_time\" content=\"2015-06-15T08:22:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-10-20T11:08:30+00:00\" \/>\n<meta name=\"author\" content=\"M\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Geschrieben von\" \/>\n\t<meta name=\"twitter:data1\" content=\"M\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 Minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\"},\"author\":{\"name\":\"M\",\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/01159effaa2678b9655388109ddf07f0\"},\"headline\":\"Swift: How to input a number from CLI Command Line Interface\",\"datePublished\":\"2015-06-15T08:22:40+00:00\",\"dateModified\":\"2016-10-20T11:08:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\"},\"wordCount\":175,\"commentCount\":0,\"articleSection\":[\"Swift\"],\"inLanguage\":\"de-CH\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\",\"url\":\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\",\"name\":\"Swift: How to input a number from CLI Command Line Interface - Flagsoft\",\"isPartOf\":{\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/#website\"},\"datePublished\":\"2015-06-15T08:22:40+00:00\",\"dateModified\":\"2016-10-20T11:08:30+00:00\",\"author\":{\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/01159effaa2678b9655388109ddf07f0\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#breadcrumb\"},\"inLanguage\":\"de-CH\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.flagsoft.com\/cmswp\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift: How to input a number from CLI Command Line Interface\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/#website\",\"url\":\"https:\/\/www.flagsoft.com\/cmswp\/\",\"name\":\"Flagsoft\",\"description\":\"ALPHA Schulmagazin, Byztxt, Strongsdict, Remote Control Cursor for Mac, PictureViewer4Mac, sdspeed free SD Card Reader for Mac OS X, Byztxt Light, 1A Shopping List, Kindle Book Template\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.flagsoft.com\/cmswp\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"de-CH\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/01159effaa2678b9655388109ddf07f0\",\"name\":\"M\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de-CH\",\"@id\":\"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3eea8d3678df652d127d9fa625eacfd1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3eea8d3678df652d127d9fa625eacfd1?s=96&d=mm&r=g\",\"caption\":\"M\"},\"url\":\"https:\/\/www.flagsoft.com\/cmswp\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift: How to input a number from CLI Command Line Interface - Flagsoft","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/","og_locale":"de_DE","og_type":"article","og_title":"Swift: How to input a number from CLI Command Line Interface - Flagsoft","og_description":"Is there no input() function in Swift? No. Not for now. But how then can we enter a number or a string from the command line interface (CLI)? 1. Copy this function to your code: func input() -&gt; String { \u00a0\u00a0 \u00a0var keyboard = NSFileHandle.fileHandleWithStandardInput() \u00a0\u00a0 \u00a0var inputData = keyboard.availableData \u00a0\u00a0 \u00a0var strData = NSString(data: &hellip; Swift: How to input a number from CLI Command Line Interface Weiterlesen &raquo;","og_url":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/","og_site_name":"Flagsoft","article_published_time":"2015-06-15T08:22:40+00:00","article_modified_time":"2016-10-20T11:08:30+00:00","author":"M","twitter_card":"summary_large_image","twitter_misc":{"Geschrieben von":"M","Gesch\u00e4tzte Lesezeit":"1 Minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#article","isPartOf":{"@id":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/"},"author":{"name":"M","@id":"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/01159effaa2678b9655388109ddf07f0"},"headline":"Swift: How to input a number from CLI Command Line Interface","datePublished":"2015-06-15T08:22:40+00:00","dateModified":"2016-10-20T11:08:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/"},"wordCount":175,"commentCount":0,"articleSection":["Swift"],"inLanguage":"de-CH","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/","url":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/","name":"Swift: How to input a number from CLI Command Line Interface - Flagsoft","isPartOf":{"@id":"https:\/\/www.flagsoft.com\/cmswp\/#website"},"datePublished":"2015-06-15T08:22:40+00:00","dateModified":"2016-10-20T11:08:30+00:00","author":{"@id":"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/01159effaa2678b9655388109ddf07f0"},"breadcrumb":{"@id":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#breadcrumb"},"inLanguage":"de-CH","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.flagsoft.com\/cmswp\/swift-how-to-input-a-number-from-cli-command-line-interface\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.flagsoft.com\/cmswp\/"},{"@type":"ListItem","position":2,"name":"Swift: How to input a number from CLI Command Line Interface"}]},{"@type":"WebSite","@id":"https:\/\/www.flagsoft.com\/cmswp\/#website","url":"https:\/\/www.flagsoft.com\/cmswp\/","name":"Flagsoft","description":"ALPHA Schulmagazin, Byztxt, Strongsdict, Remote Control Cursor for Mac, PictureViewer4Mac, sdspeed free SD Card Reader for Mac OS X, Byztxt Light, 1A Shopping List, Kindle Book Template","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.flagsoft.com\/cmswp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"de-CH"},{"@type":"Person","@id":"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/01159effaa2678b9655388109ddf07f0","name":"M","image":{"@type":"ImageObject","inLanguage":"de-CH","@id":"https:\/\/www.flagsoft.com\/cmswp\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3eea8d3678df652d127d9fa625eacfd1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3eea8d3678df652d127d9fa625eacfd1?s=96&d=mm&r=g","caption":"M"},"url":"https:\/\/www.flagsoft.com\/cmswp\/author\/admin\/"}]}},"jetpack_featured_media_url":"","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"yarpp-thumbnail":false,"slide-thumbnail":false},"uagb_author_info":{"display_name":"M","author_link":"https:\/\/www.flagsoft.com\/cmswp\/author\/admin\/"},"uagb_comment_info":12,"uagb_excerpt":"Is there no input() function in Swift? No. Not for now. But how then can we enter a number or a string from the command line interface (CLI)? 1. Copy this function to your code: func input() -&gt; String { \u00a0\u00a0 \u00a0var keyboard = NSFileHandle.fileHandleWithStandardInput() \u00a0\u00a0 \u00a0var inputData = keyboard.availableData \u00a0\u00a0 \u00a0var strData = NSString(data:&hellip;","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/posts\/1829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/comments?post=1829"}],"version-history":[{"count":0,"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/posts\/1829\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/media?parent=1829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/categories?post=1829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flagsoft.com\/cmswp\/wp-json\/wp\/v2\/tags?post=1829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}