gpt4 book ai didi

ios - 将字符串条目从 Swift iOS 中的 UITextField 保存到 plist

转载 作者:行者123 更新时间:2023-11-30 13:22:06 25 4
gpt4 key购买 nike

我想知道如何获取输入到 UItextField 中的值并将其保存到 Plist 文件 strIng 条目中,其中键为“customText”。

有人知道我在这里做错了什么吗?

这是我当前的 View Controller :

//
// DetailViewController.swift
// ConversationalCards
//
// Created by Avi Tannenbaum on 3/20/16.
// Copyright © 2016 Avi Tannenbaum. All rights reserved.
//

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var modeLabel: UILabel!
@IBOutlet weak var customTextField: UITextField!
@IBAction func ifTextChanged(sender: AnyObject) {

if case customTextField.text {
card?.customText = customTextField.text
}
}

var card: Card?

override func viewDidLoad() {
super.viewDidLoad()

if let card = card {
navigationItem.title = card.name?.capitalizedString
imageView.image = UIImage(named: card.name!.lowercaseString)
titleLabel.text = card.title?.capitalizedString

//Mode label
modeLabel.text = card.mode?.capitalizedString

self.setStyle()

// get custom text from text field
// Color the text backgrounds and then resize them later for the cards
//Red
switch card.color ?? "none" { // default to none
case "0":
titleLabel.backgroundColor = UIColor(red:0.93, green:0.33, blue:0.36, alpha:1.0)
// color hex code #EE535D
case "1":
titleLabel.backgroundColor = UIColor(red:0.29, green:0.74, blue:0.70, alpha:1.0)
// color hex code #4ABCB2
case "2":
titleLabel.backgroundColor = UIColor(red:0.92, green:0.65, blue:0.09, alpha:1.0)
// color hex code #EAA516
case "3":
titleLabel.backgroundColor = UIColor(red:0.15, green:0.40, blue:0.57, alpha:0.3)
titleLabel.textColor = UIColor(red:1.00, green:1.00, blue:0.95, alpha:1.0)
// color hex code #276592
case "4":
titleLabel.backgroundColor = UIColor(red:0.65, green:0.80, blue:0.39, alpha:1.0)
// color hex code #A6CC63
case "5":
titleLabel.backgroundColor = UIColor(red:0.78, green:0.30, blue:0.35, alpha:1.0)
// color hex code #C84D59
case "6":
titleLabel.backgroundColor = UIColor(red:0.40, green:0.34, blue:0.54, alpha:1.0)
// color hex code #67578A
default:
titleLabel.hidden = false
}

// Label style
// self.titleLabelStyle()
}
}



// font style

func setStyle() {
// Script in charge of main label text font and text size
titleLabel.font = UIFont(name: "TrajanPro3-Regular", size: 26.0)
titleLabel.font = UIFont.boldSystemFontOfSize(26)
titleLabel.font = UIFont.italicSystemFontOfSize(26)
titleLabel.font = UIFont.systemFontOfSize(26)
}


/* not working yet this is the function responseble for converting hex values to UIColor values - Avi Tannenbaum
extension UIColor {
public convenience init?(hexString: String) {
let r, g, b, a: CGFloat

if hexString.hasPrefix("#") {
let start = hexString.startIndex.advancedBy(1)
let hexColor = hexString.substringFromIndex(start)

if hexColor.characters.count == 8 {
let scanner = NSScanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexLongLong(&hexNumber) {
r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
g = CGFloat((hexNumber & 0X00FF0000) >> 16) / 255
b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
a = CGFloat(hexNumber & 0x000000ff) / 255

self.init(red: r, green: g, blue: b, alpha: a)
return
}
}
}
return nil
}

} */


// Style functions
/*
func titleLabelStyle() {
let title = self.titleLabel
title.numberOfLines = 0
title.frame = CGRect(x: 10, y: 10, width: 300, height: title.intrinsicContentSize().height)
}


*/

}

这是我的 plist 文件

<pre><code><blockquote>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>Begin</string>
<key>title</key>
<string> </string>
<key>mode</key>
<string> </string>
<key>color</key>
<string>none</string>
<key>group</key>
<string>Getting Started</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>THE COMPLIMENT
I WANT TO GIVE YOU...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>TALK ABOUT YOUR NEXT VACATION...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I THINK OUR BEST MEMORY IS...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>AN ADVENTURE I WANT TO SHARE WITH YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I TRUST YOU BECAUSE...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I FELL IN LOVE WITH YOU WHEN...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>RECEIVE A HUG...</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I REALLY APPRECIATE WHEN YOU...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>You MAKE ME REALLY HAPPY WHEN...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I BELIEVE IN YOU BECAUSE...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>SOMETHING SILLY l WANT TO DO WITH YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT TO PAMPER YOU BY...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>A SMALL DOSE OF ROMANCE WOULD INCLUDE...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>HOLD HANDS WITH EYES CLOSED...</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>A GIFT I WOULD LIKE TO RECEIVE FROM YOU...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE THING I HAVE LEARNED FROM YOU...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT TO SHOW YOU SUPPORT BY...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>WHAT IS YOUR FAVOURITE ROMANTIC MOVIE? WHY?</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>MY ATTRACTION TO YOU DEEPENED WHEN...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I Think OUR RELATIONSHIP IS AT ITS BEST WHEN...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>WRITE YOUR PARTNER A THANKYOU NOTE...</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Four</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WAS FIRST ATTRACTED TO YOU BECAUSE...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>You BRING OUT THE BEST IN ME BY...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I&apos;D LIKE TO STUDY _ WITH YOU.</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>SOMETHING CRAZY I WANT TO DO WITH YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE WAY TO TREAT OURSELVES TOGETHER...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I THINK OF US WHEN l HEAR THIS SONG...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>RECEIVE A HEAD MASSAGE.</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>MY EARLIEST MEMORY OF OUR FRIENDSHIP...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>SOMETHING EXCITING WE CAN DO TOGETHER...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>OUR IDEAL DAY TOGETHER...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>IF I COULD BUY YOU ANYTHING I WOULD BUY YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I FEEL CLOSE TO YOU WHEN...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>DESCRIBE A ROMANTIC EVENING</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>WRITE YOUR PARTNER A COMPLIMENT NOTE.</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>THE TALENT OF YOURS I REALLY LIKE...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date Six</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I ADMIRE THAT YOU...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date Six</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT TO THANK YOU FOR...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date Six</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>A SURPRISE I&apos;D LIKE FROM YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date Six</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I LOVE YOU BECAUSE...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date Six</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>MY EARLIEST MEMORY OF OUR ROMANCE...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date Six</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>RECEIVE A SHOULDER MASSAGE</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date Six</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE OF YOUR STRENGTHS...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE PLEASURE I WOULD LIKE TO GIVE YOU...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>OUR LOVE REMINDS ME OF THIS CANDY...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I FEEL SAFE WITH YOU BECAUSE...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT YOU TO PAMPER ME BY...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I THINK YOU ARE SEXY BECAUSE...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
<key>customText</key>
<string></string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>LOOK INTO EACH OTHERS EYES.</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
<key>customText</key>
<string></string>
</dict>

最佳答案

您好,我认为您需要将您的 Card 类转换为 Dictionary 然后您需要将此字典保存到 plist,为此您必须放置此方法在你的卡片类别中。请注意,如果您使用某个类作为此 Card 类的属性,则必须将此类也转换为字典

func toDictionary() -> NSDictionary
{
let returnDict = NSMutableDictionary();
returnDic.setObject(title, forKey: "title");
returnDic.setObject(name, forKey: "name");
//**PUT ALL YOUR ATTRIBUTES IN THE DICTIONARY//

return returnDict;
}

然后你需要循环你的卡片并将你的字典放入一个数组中,然后你必须将该数组传递给这个方法

static func saveArray(array:NSArray,filename:String) ->Bool
{
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true);
let docsDirectory = paths[0];
let path = docsDirectory + "/" + filename;
return array.writeToFile(path, atomically: true);
}

希望这对你有帮助

关于ios - 将字符串条目从 Swift iOS 中的 UITextField 保存到 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com