<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alberto Giambone 👨‍🌾</title>
    <description>The latest articles on DEV Community by Alberto Giambone 👨‍🌾 (@albertogiambone).</description>
    <link>https://dev.to/albertogiambone</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F545221%2F55d048e7-2921-441f-97c5-4ca4d658b6ef.jpg</url>
      <title>DEV Community: Alberto Giambone 👨‍🌾</title>
      <link>https://dev.to/albertogiambone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/albertogiambone"/>
    <language>en</language>
    <item>
      <title>Split Array separated by...</title>
      <dc:creator>Alberto Giambone 👨‍🌾</dc:creator>
      <pubDate>Mon, 22 Aug 2022 20:44:40 +0000</pubDate>
      <link>https://dev.to/albertogiambone/split-array-separated-by-1cj7</link>
      <guid>https://dev.to/albertogiambone/split-array-separated-by-1cj7</guid>
      <description>&lt;p&gt;Hi,&lt;br&gt;
my way to split one array (of String) in two or more &lt;strong&gt;var&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;sometimes you have a Date() in a String format, and you want only the year.&lt;/p&gt;

&lt;p&gt;you can use &lt;strong&gt;.components(separatedBy: "/")&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func splitArray() {

    let myARRAY = "20/09/2021"
    let SplittedArray = myARRAY.components(separatedBy: "/")

    let DAY = SplittedArray[0]
    let MONTH = SplittedArray[1]
    let YEAR = SplittedArray[2]

        print("\(DAY), \(MONTH), \(YEAR)")
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Firestore Swift CRUD</title>
      <dc:creator>Alberto Giambone 👨‍🌾</dc:creator>
      <pubDate>Mon, 22 Aug 2022 20:34:00 +0000</pubDate>
      <link>https://dev.to/albertogiambone/firestore-swift-crud-1pmd</link>
      <guid>https://dev.to/albertogiambone/firestore-swift-crud-1pmd</guid>
      <description>&lt;p&gt;My way to Firestore CRUD for iOS UIKit🥳,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Create a Rule for your data on the Firestore Rule page&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    match /Adress/{userId} {
      allow read, write: if request.auth != null;
      allow create: if request.auth != null;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Create a Model for your data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I usually use &lt;strong&gt;Struct&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;create a swift file -&amp;gt; and write down you struct&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Struct myStruct {

    var name: String
    var city: String
    var state: String
    var UID: String
    var DID: String

    var dict: [String: Any] {
        return[ 
        "name": name,
        "city": city,
        "state": state,
        "UID": UID,
        "DID": DID
       ]
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Create a UserDefaults Object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After user Login save the user ID on &lt;strong&gt;UserDefaults&lt;/strong&gt; as String, then reuse this element in &lt;strong&gt;ViewDidLoad()&lt;/strong&gt; or &lt;strong&gt;ViewWillAppear()&lt;/strong&gt; resuming this value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Create a your first Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Save to Firestore with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let db = Firestore.firestore()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;then...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; db.collection("Adress").addDocument(data: [

            "name": String(name_label.text ?? ""),
            "city": String(city_label.text ?? ""),
            "state": String(state_label.text ?? ""),
            "UID": String(ID ?? ""),
            "DID": String("")
        ]) { err in
            if let err = err {
                print("Error adding document: \(err)")
            } else {
                //print("Document added with ID: \(ref.documentID)")
                }
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Read Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remember to use the user Identifier you already saved in UserDefault, I usually do this in &lt;strong&gt;ViewWillAppear()&lt;/strong&gt; beacuse of hierarchies...&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ID = UserDefaults.standard.object(forKey: "userInfo") as? String&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;create an array of your struct for your data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var arrayOfData = [myStruct]()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then you start read from &lt;strong&gt;Firestore&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let db = Firestore.firestore()

        db.collection("Adress").whereField("UID", isEqualTo: ID!).getDocuments() { [self](querySnapshot, err) in

            if let err = err {
                print("Error getting Firestore data: \(err)")
            }else{
                for document in querySnapshot!.documents {

                let singleData = myStruct(name: document.data()["name"] as? String ?? "", city: document.data()["city"] as? String ?? "", state: document.data()["state"] as? String ?? "",
UID: document.data()["UID"] as? String ?? "",
DID: document.documentID)

self.arrayOfData.append(singleData)
            }
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Update Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Updating data by &lt;strong&gt;documentID&lt;/strong&gt; and of course user ID 😂&lt;/p&gt;

&lt;p&gt;when have you found the right document to update, set the new value for the document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let DOCREFERENCE = db.collection("Adress").document(documentID!)

            DOCREFERENCE.setData([
                "name": String(name_label.text ?? ""),
            "city": String(city_label.text ?? ""),
            "state": String(state_label.text ?? ""),
            "UID": String(ID ?? ""),
            "DID": String(documentID)
            ])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Delete Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Delete data in a collection (usually UITableView() or UICollectionView())&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let db = Firestore.firestore()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db.collection("Adress").document(documentID).delete()&lt;/code&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>firebase</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Replace String part on Swift</title>
      <dc:creator>Alberto Giambone 👨‍🌾</dc:creator>
      <pubDate>Wed, 27 Jul 2022 14:36:40 +0000</pubDate>
      <link>https://dev.to/albertogiambone/replace-string-part-on-swift-22jb</link>
      <guid>https://dev.to/albertogiambone/replace-string-part-on-swift-22jb</guid>
      <description>&lt;p&gt;HI all,&lt;br&gt;
some years ago i needed to substitute "," with "." in a String.&lt;/p&gt;

&lt;p&gt;I used this func :&lt;br&gt;
&lt;code&gt;extension String {&lt;br&gt;
    mutating func replace(_ originalString:String, with newString:String) {&lt;br&gt;
        self = self.replacingOccurrences(of: originalString, with: newString)&lt;br&gt;
    }&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;but today I discovered Swift has a Method &lt;a href="https://developer.apple.com/documentation/foundation/nsstring/1412937-replacingoccurrences"&gt;replacingOccurrences&lt;/a&gt; in a String 🥳🥳&lt;/p&gt;

</description>
      <category>swift</category>
      <category>programming</category>
      <category>ios</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
