{"id":674,"date":"2015-08-13T18:46:29","date_gmt":"2015-08-13T18:46:29","guid":{"rendered":"http:\/\/phyline.com\/blog\/?p=674"},"modified":"2016-02-28T11:55:48","modified_gmt":"2016-02-28T11:55:48","slug":"como-usar-removeobjectatindex-de-un-array-en-swift","status":"publish","type":"post","link":"http:\/\/sophyonline.com\/blog\/2015\/08\/como-usar-removeobjectatindex-de-un-array-en-swift\/","title":{"rendered":"\u00bfC\u00f3mo usar removeObjectAtIndex de un Array en Swift?"},"content":{"rendered":"<p><a href=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2015\/08\/swift-e1456593526993.png\" rel=\"attachment wp-att-685\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-685\" src=\"http:\/\/sophyonline.com\/blog\/wp-content\/uploads\/2015\/08\/swift-e1456593526993.png\" alt=\"swift\" width=\"900\" height=\"230\" \/><\/a><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">Para emplear el removeAtIndex, que permite eliminar un elemento contenido en un array, del cual no sabemos su \u00edndice, hay que hacer un par de maromas en Swift 1.2.<\/span><\/p>\n<p class=\"para\"><span style=\"font-family: helvetica, arial, sans-serif;\">Veamos el c\u00f3digo en Objective-c:<\/span><\/p>\n<h2><span style=\"font-family: helvetica, arial, sans-serif;\">Objective-C:<\/span><\/h2>\n<pre><span style=\"font-family: helvetica, arial, sans-serif;\">[self.selectedRows removeObject:[NSNumber numberWithLong:indexPath.row]];<\/span><\/pre>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">Resulta sencillo eliminar directamente de un array (<strong>selectedRows<\/strong>) de tipo mutable, es decir un NSMutableArray, un elemento contenido en \u00e9l empleando <strong>removeObject<\/strong>.<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">\u00bfY qu\u00e9 tenemos que hacer en Swift?:<\/span><\/p>\n<h2><span style=\"font-family: helvetica, arial, sans-serif;\">\u00a0<\/span><\/h2>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\"><!--more--><\/span><\/p>\n<h2><span style=\"font-family: helvetica, arial, sans-serif;\">Swift:<\/span><\/h2>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">Algo tan sencillo como emplear la funci\u00f3n <strong>find <\/strong>(esto debido a que nuestro tipo es Array)<strong>, <\/strong>donde\u00a0<strong>indexPath.row<\/strong>\u00a0es el objeto que vamos a\u00a0eliminar del array <strong>selectedRows<\/strong>:<\/span><\/p>\n<pre><span style=\"font-family: helvetica, arial, sans-serif;\">if let index = find(selectedRows, indexPath.row){\r\n selectedRows.removeAtIndex(index)\r\n }\r\n<\/span><\/pre>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">\u00bfPor qu\u00e9 resulta tan extra\u00f1o realizar la misma tarea en Swift?. Primero hay que entender de qu\u00e9 tipo son los Array en Swift. \u00bfes un equivalente a NSArray?. \u00a0Seg\u00fan Apple, efectivamente nos cuenta que un Array es el equivalente a la clase NSArray de Foundation.<\/span><\/p>\n<blockquote><p><span style=\"font-family: helvetica, arial, sans-serif;\"><a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/CollectionTypes.html\">Swift\u2019s <code class=\"code-voice\">Array<\/code> type is bridged to Foundation\u2019s <code class=\"code-voice\">NSArray<\/code> class<\/a>.<\/span><\/p><\/blockquote>\n<div class=\"note\"><span style=\"font-family: helvetica, arial, sans-serif;\">\u00a0<\/span><\/div>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">As\u00ed que, dado que los <strong>NSArray<\/strong> son inmutables, no es posible realizar un <strong>removeObject<\/strong>, ya que \u00e9ste m\u00e9todo le pertenece a <strong>NSMutableArray,<\/strong>\u00a0sublcase de NSArray. Por lo que terminamos empleando un <strong>find<\/strong> para luego hacer un <strong>removeAtIndex.<\/strong><\/span><\/p>\n<h2><span style=\"font-family: helvetica, arial, sans-serif;\">Extension<\/span><\/h2>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">Pero, esto no acaba aqu\u00ed; para facilitarnos la vida cada vez que queramos eliminar de un Array en Swift, podemos incluso hacer algo mejor, crear una extensi\u00f3n -Ojo, tambi\u00e9n podr\u00edamos\u00a0hacer mutable el Array empleando un NSMutableArray desde el principio, asumiendo que queremos eliminar o insertar objetos en \u00e9l que para eso se usan los NSMutableArray, pero, as\u00ed no aprender\u00edamos a hacer estas cositas, \u00bfno?-<\/span><\/p>\n<pre class=\"p1\"><span style=\"font-family: helvetica, arial, sans-serif;\"><span class=\"s1\">\r\nimport<\/span><span class=\"s2\"> Foundation\r\n\r\n<\/span><span class=\"s2\">extension<\/span> <span class=\"s4\">Array<\/span><span class=\"s3\"> {\r\n<\/span><span class=\"s1\">   mutating<\/span> <span class=\"s1\">func<\/span><span class=\"s2\"> removeObject&lt;T : Equatable&gt;(object: <\/span><span class=\"s4\">T<\/span><span class=\"s2\">, array: [<\/span><span class=\"s4\">T<\/span><span class=\"s2\">]) {<\/span><span class=\"s2\">\u00a0\r\n<\/span><span class=\"s2\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 <\/span><\/span><span class=\"s1\">if<\/span> <span class=\"s1\">let<\/span><span class=\"s2\"> index = <\/span><span class=\"s4\">find<\/span><span class=\"s2\">(array, object){\r\n<\/span><span class=\"s2\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><\/span><span class=\"s1\">self<\/span><span class=\"s2\">.<\/span><span class=\"s4\">removeAtIndex<\/span><span class=\"s2\">(index)\r\n<\/span><span class=\"s2\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>}\r\n<\/span><span class=\"s2\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\r\n<\/span><span class=\"s2\">}<\/span><\/span><\/pre>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">Nota que hemos tenido que emplear la opci\u00f3n<strong> Equatable T<\/strong>, lo que significa, que podemos recibir cualquier tipo (String, Integer, AnyObject) como objeto (el que terminaremos eliminando). Y puesto que <strong>find<\/strong> no funciona empleando <strong>self<\/strong> (cuyo tipo es un\u00a0Array) si no especificamos\u00a0concretamente que es un array con un tipo especifico, se lo pasamos por par\u00e1metro obligatoriamente por lo que empleamos <strong>[T].<\/strong> Al mismo tiempo, nos encargamos de que la funci\u00f3n find, si no encuentra nada no pase por la funci\u00f3n self.removeAtIndex, lo que nos ahorra una Excepci\u00f3n.<\/span><\/p>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">Tambi\u00e9n observamos que empleamos <strong>mutating <\/strong>al inicio de\u00a0la funci\u00f3n, lo que le permite realizar cambios en sus propiedades cada vez que se usa:<\/span><\/p>\n<blockquote><p><span style=\"font-family: helvetica, arial, sans-serif;\">\u00a0<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/Methods.html\">if you need to modify the properties of your structure or enumeration within a particular method, you can opt in to\u00a0<em>mutating<\/em>\u00a0behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends.<\/a><\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-family: helvetica, arial, sans-serif;\">Finalmente, \u00bfC\u00f3mo lo usamos?<\/span><\/h2>\n<pre class=\"p1\"><span style=\"font-family: helvetica, arial, sans-serif;\"><span class=\"s2\">var<\/span><span class=\"s1\"> shoppingList: [<\/span><span class=\"s3\">String<\/span><span class=\"s1\">] = [<\/span><span class=\"s4\">\"Eggs\"<\/span><span class=\"s1\">, <\/span><span class=\"s4\">\"Milk\"<\/span><span class=\"s1\">]\r\n<\/span><span class=\"s2\">shoppingList<\/span><span class=\"s1\">.<\/span><span class=\"s2\">removeObject<\/span><span class=\"s1\">(<\/span><span class=\"s3\">\"Eggs\"<\/span><span class=\"s1\">, array: <\/span><span class=\"s2\">shoppingList<\/span><span class=\"s1\">)\r\n\r\n<\/span><\/span><\/pre>\n<p><span style=\"font-family: helvetica, arial, sans-serif;\">Pues nada, \u00a1sigamos aprendiendo Swift!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Para emplear el removeAtIndex, que permite eliminar un elemento contenido en un array, del cual no sabemos su \u00edndice, hay que hacer un par de maromas en Swift 1.2. Veamos el c\u00f3digo en Objective-c: Objective-C: [self.selectedRows removeObject:[NSNumber numberWithLong:indexPath.row]]; Resulta sencillo eliminar directamente de un array (selectedRows) de tipo mutable, es decir un NSMutableArray, un elemento <a class=\"read-more\" href=\"http:\/\/sophyonline.com\/blog\/2015\/08\/como-usar-removeobjectatindex-de-un-array-en-swift\/\">&hellip;&nbsp;<span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":17,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":721,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions\/721"}],"wp:attachment":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}