Mobile App Dev 2022W: Tutorial 2: Difference between revisions

From Soma-notes
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''This tutorial is still being developed.'''
In this tutorial we'll be playing with <tt>textanalyzer-1</tt>, a very simple app for analyzing text that shows off a number of features of Swift and SwiftUI.
In this tutorial we'll be playing with <tt>textanalyzer-1</tt>, a very simple app for analyzing text that shows off a number of features of Swift and SwiftUI.


Line 25: Line 23:
# Move the analysis menu to the top of the screen.
# Move the analysis menu to the top of the screen.
# Add a menu option "Empty" that returns "Yes" or "No" based on whether the input string is empty or not.  Define it using a closure/anonymous function.
# Add a menu option "Empty" that returns "Yes" or "No" based on whether the input string is empty or not.  Define it using a closure/anonymous function.
# Add a menu option "Pet Mentions" that counts the number of occurrences of the words "Shift", "Tab" or "Roshi", or any other pets you like.  Define this using a named function "countPets".
# Add a menu option "Pets Mentioned" that counts how many of the words "Shift", "Tab" or "Roshi" (or any other pets you like) appears in the text.  Multiple occurrences of the same pet should only be counted once.  Define this using a named function "countPetsMentioned".  (Hint: try the string "contains" method.)


==Code==
==Code==
Line 63: Line 61:
                 mode in
                 mode in
                 Button(mode, action: {
                 Button(mode, action: {
                     analysisMode = mode;
                     analysisMode = mode
                 })
                 })
             }
             }
Line 72: Line 70:


func countUpper(_ s: String) -> String {
func countUpper(_ s: String) -> String {
     var count = 0;
     var count = 0
     let upperCase: Set<Character> =
     let upperCase: Set<Character> =
     ["A","B","C","D","E","F","G","H","I","J","K","L","M",
     ["A","B","C","D","E","F","G","H","I","J","K","L","M",
     "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
     "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
      
      
     for c in s {
     for c in s {
         if (upperCase.contains(c)) {
         if (upperCase.contains(c)) {
             count += 1;
             count += 1
         }
         }
     }
     }

Latest revision as of 06:11, 19 January 2022

In this tutorial we'll be playing with textanalyzer-1, a very simple app for analyzing text that shows off a number of features of Swift and SwiftUI.

Getting Started

First, create a multiplatform app called "textanalyzer-1". Replace the contents of ContentView.swift with the code below.

Resources

Questions

  1. This code defines two views. What are they? How are they connected to each other?
  2. What kind of data structure is analysis? What data does it contain?
  3. What is the difference between the ForEach and for-in constructs? Are they interchangeable?
  4. How does countUpper figure out whether a character is upper case or not?

Tasks

  1. Move the analysis menu to the top of the screen.
  2. Add a menu option "Empty" that returns "Yes" or "No" based on whether the input string is empty or not. Define it using a closure/anonymous function.
  3. Add a menu option "Pets Mentioned" that counts how many of the words "Shift", "Tab" or "Roshi" (or any other pets you like) appears in the text. Multiple occurrences of the same pet should only be counted once. Define this using a named function "countPetsMentioned". (Hint: try the string "contains" method.)

Code

ContentView.swift for textanalyzer-1

//
//  ContentView.swift for textanalyzer-1
//
//  Created by Anil Somayaji on 2022-01-19.
//

import SwiftUI

struct ContentView: View {
    @State private var t = ""
    @State private var analysisMode = "Count"
    var body: some View {
        VStack{
            Text("Text Analyzer").bold().padding()
            TextField("Enter Text", text: $t).padding()
            Text(analysisMode + ": " + analysis[analysisMode]!(t)).padding()
            ModeMenu(analysisMode: $analysisMode)
        }
    }
}

struct ModeMenu: View {
    @Binding var analysisMode: String

    var body: some View {
        let availModes = [String](analysis.keys)

        Menu("Analysis menu") {
            ForEach(availModes, id: \.self) {
                mode in
                Button(mode, action: {
                    analysisMode = mode
                })
            }
        }

    }
}

func countUpper(_ s: String) -> String {
    var count = 0
    let upperCase: Set<Character> =
    ["A","B","C","D","E","F","G","H","I","J","K","L","M",
     "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    
    for c in s {
        if (upperCase.contains(c)) {
            count += 1
        }
    }
    
    return String(count);
}

let analysis: [String: (String) -> String] = [
    "Count": {s in return String(s.count)},
    "Upper Case": countUpper
]

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}