{"id":1677,"date":"2013-05-21T13:28:57","date_gmt":"2013-05-21T20:28:57","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1677"},"modified":"2013-06-01T15:52:30","modified_gmt":"2013-06-01T22:52:30","slug":"finding-substrings-in-ios","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1677","title":{"rendered":"Finding substrings in iOS"},"content":{"rendered":"<p>It one point I experimented with using titles on my answer buttons to keep track of which answer was selected. I switched to tags since they are integers and they are easier to use in this particular implementation. In any case, I though it interesting to show how to get a substring from a string. My titles are of the form, &#8216;Answer1&#8217;, &#8216;Answer2&#8217;, &#8230; &#8216;Answer16&#8242;. I really only need the last one or two digits. I pass an NSString into the delegate method from a button press.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\n- (void)checkBoxTapped:(UIButton *)buttonPressed {\n&nbsp;&nbsp;&nbsp;&nbsp;[[self delegate] quizAnswered:buttonPressed.currentTitle];\n}\n<\/code><\/pre><\/p>\n<p>Then I grab either the last digit or the last two digits from the string.<\/p>\n<p><pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSString *answer = [selectedAnswer substringWithRange:NSMakeRange(6, 1)]; \/\/ wrongAnswer - wA\nif (selectedAnswer.length == 8) wA = [selectedAnswer substringWithRange:NSMakeRange(6, 2)];\n<\/code><\/pre><\/p>\n<p>I was doing a string comparison to decide which question was answered,<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nif ([answer isEqualToString:@&quot;1&quot;] || \n&nbsp;&nbsp;&nbsp;&nbsp;[answer isEqualToString:@&quot;2&quot;] || \n&nbsp;&nbsp;&nbsp;&nbsp;[answer isEqualToString:@&quot;3&quot;] || \n&nbsp;&nbsp;&nbsp;&nbsp;[answer isEqualToString:@&quot;4&quot;] ) {\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ do something\n}\n<\/code><\/pre><\/p>\n<p>What I should have done is convert the string to an int and then do a simple comparison.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSInteger answerAsInt = [answer intValue];\nif ([answerAsInt &lt; 5 ) {\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ do something\n}\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It one point I experimented with using titles on my answer buttons to keep track of which answer was selected. I switched to tags since they are integers and they are easier to use in this particular implementation. In any case, I though it interesting to show how to get a substring from a string. &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1677\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Finding substrings in iOS<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1677","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1677"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1677\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}