【Swift】構造体を受け取り、関数に渡し、メソッドを作成するソースコード

この記事は約7分で読めます。
Xcodeで構造体を定義してみましょう。
作り方や方法、ソースコードは次に記載しております。
Swiftでプログラミングしてきましょう。
スポンサーリンク

構造体を定義するためのソースコード

struct Human{
var Name: String
var Age: Int!
var Address: String!
func print() -> String{
return "¥(Name) ¥(Age)¥(Address)"
}
}

class UpdownViewController{
@IBAction func clickButton(sender: AnyObject){
var hu = Human(Name: "Updown", Age: 32, Address: "Fukuoka")
text1.text = hu.print()
}
}
スポンサーリンク

構造体を使うためのソースコード(init)

struct Human{
var Name: String
var Age: Int!
var Address: String!
func print() -> String{
return "¥(Name) ¥(Age)¥(Address)"
}
init(){
self.Name = "none"
}
}

class UpdownViewController: UIViewController{
@IBOutlet weak var text1: UITextField!
@IBAction func clickButton(sender: AnyObject){
var hu = Human()
hu.Name = "Updown"
hu.Age = 32
hu.Address = "Fukuoka"
text1.text = hu.print()
}
}
スポンサーリンク

構造体を配列で使うソースコード(Array)

struct Human{
var Name: String
var Age: Int!
var Address: String!
func print() -> String{
return "¥(Name) ¥(Age)¥(Address)"
}
init(){
self.Name = "none"
}
init(name:String, age:Int, address:String){
self.Name = name
self.Age = age
self.Address = address
}
}

class UpdownViewController: UIViewController{
@IBAction func clickButton(sender: AnyObject){
var lst = [Human]()
lst.append(Human(name: "Updown1", age: 32, address: "Fukuoka"))
lst.append(Human(name: "Updown2", age: 42, address: "Ooita"))
lst.append(Human(name: "Updown3", age: 52, address: "Kumamoto"))

let texts = [text1, text2, text3]
var i = 0
for it in texts{
it.text = lst[i].print()
i++
}
}
}
スポンサーリンク

構造体を受け取り、関数に渡し、メソッドを作成するソースコード

struct Human{
  var Name: String
  var Age: Int!
  var Address: String!
  func print() -> String{
    return "¥(Name) ¥(Age)¥(Address)"
  }
  init(){
    self.Name = "none"
  }
  init(name:String, age:Int, address:String){
    self.Name = name
    self.Age = age
    self.Address = address
  }
}

class UpdownViewController: UIViewController{
  func addAge(var hu: Human) -> Human{
    if let age = hu.Age{
      hu.Age = age + 1
    }
   return hu
}

@IBAction func clickButton(sender: AnyObject){
   var lst = Array()
   lst.append(Human(name: "Updown1", age: 32, address: "Fukuoka"))
   lst.append(Human(name: "Updown2", age: 42, address: "Ooita"))
   lst.append(Human(name: "Updown3", age: 52, address: "Kumamoto"))

   let texts = [text1, text2, text3]
   var i = 0
   for it in texts{
      var hu = lst[i]
      it.text = addAge(hu).print()
      i++
    }
  }
}
スポンサーリンク

構造体を返す関数を作成するソースコード

struct Human{
  var Name: String
  var Age: Int!
  var Address: String!
  func print() -> String{
  return "¥(Name) ¥(Age)¥(Address)"
}
init(){
  self.Name = "none"
}
init(name:String, age:Int, address:String){
  self.Name = name
  self.Age = age
  self.Address = address
}
  mutaing func addAge(){
    if let age = self.Age{
      self.Age = age + 1
    }
  }
}

class UpdownViewController: UIViewController{
func makeHuman(text: String) -> Human{
  let cells = text.componentsSeparatedByString(", ")
  let name = cells[0]
  let age = Int(cells[1])
  let addr = cells[2]
  return Human(name: name, age: age!, address: addr)
}

@IBAction func clickButton(sender: AnyObject){
    var lst = Array()
    lst.append(Human(name: "Updown1", age: 32, address: "Fukuoka"))
    lst.append(Human(name: "Updown2", age: 42, address: "Ooita"))
    lst.append(Human(name: "Updown3", age: 52, address: "Kumamoto"))

    let texts = [text1, text2, text3]
    var i = 0
    for it in texts{
      it.text = lst[i].print()
      i++
    }
  }
}
タイトルとURLをコピーしました