采用以下JSON:
let rawJson =
"""
[
{
"id": 1,
"name":"John Doe"
},
{
"id": 2,
"name":"Luke Smith"
},
]
"""
Run Code Online (Sandbox Code Playgroud)
和User型号:
struct User: Decodable {
var id: Int
var name: String
}
Run Code Online (Sandbox Code Playgroud)
解码非常简单,如下所示:
let data = rawJson.data(using: .utf8)
let decoder = JSONDecoder()
let users = try! decoder.decode([User].self, from: data!)
Run Code Online (Sandbox Code Playgroud)
但是,如果JSON看起来像这样,顶级是一个字典,需要获取以下数组users:
let json =
"""
{
"users":
[
{
"id": 1,
"name":"John Doe"
},
{
"id": 2,
"name":"Luke Smith"
},
]
}
"""
Run Code Online (Sandbox Code Playgroud)
阅读JSON最有效的解决方案是什么?我绝对可以创建另一个struct这样的:
struct …Run Code Online (Sandbox Code Playgroud) 如何将邮政编码(或者在我的情况下是邮政编码(英国))转换为经纬度坐标?
我读到了使用谷歌地理编码 API,但读到它有相当多的限制,例如在应用程序中使用谷歌地图和限制请求。
我也发现了这个,但我不确定如何实施它
我有一个像这样的字符串:
var aString = "This is a string \n\n This is the second line of the string\n\n"
Run Code Online (Sandbox Code Playgroud)
textview中的内容如下所示:
This is a string
This is the second line of the string
// 2 extra lines of unnecessary white space
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来像这样:
This is a string
This is the second line of the string
Run Code Online (Sandbox Code Playgroud)
我想从字符串的末尾删除所有"\n"并删除\n,它会重复自身,因此中间没有空格.
理想情况下,我猜测最终结果应该是这样的:
var aString = "This is a string \n This is the second line of the string"
Run Code Online (Sandbox Code Playgroud) 我有2个单元格的tableview.两个单元格都有文本字段和标签.Textfield采用用户名/电子邮件地址和密码的值.
var emailAddress: String?
var password: String?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! TextFieldCell
cell.label.text = loginOptions[indexPath.row]
cell.textField.placeholder = loginOptions[indexPath.row]
if indexPath.row == 0 {
emailAddress = cell.textField.text!
}
if indexPath.row == 1 {
password = cell.textField.text!
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
下面是检查文本框中是否有任何文本的功能,如果没有,则显示建议填写所有字段的警报控制器.
func signInButtonTapped() {
if emailAddress!.isEmpty || password!.isEmpty {
let alert = UIAlertController(title: "Alert", message: "all fields are required to be filled in", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: …Run Code Online (Sandbox Code Playgroud) 我有一个带有3个标签栏按钮的标签栏控制器.目前它看起来像这样:
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let firstTabBarController = FirstController()
let firstTabBarNavigationController = UINavigationController(rootViewController: firstTabBarController)
firstTabBarNavigationController.tabBarItem.title = "First Tab"
let secondTabBarController = SecondController()
let secondTabBarNavigationController = UINavigationController(rootViewController: secondTabBarController)
secondTabBarController.tabBarItem.title = "Second Tab"
let thirdTabBarController = ThirdController()
let thirdTabBarNavigationController = UINavigationController(rootViewController: thirdTabBarController)
thirdTabBarNavigationController.tabBarItem.title = "Third Tab"
viewControllers = [firstTabBarNavigationController, secondTabBarNavigationController, thirdTabBarNavigationController]
}
}
Run Code Online (Sandbox Code Playgroud)
现在,使用上面的代码,所有视图控制器都位于内部 CustomTabBarController
我想要中间的标签栏按钮,secondTabBarNavigationController用于呈现视图控制器,特别UIImagePickerController是用户选择图像,类似于Instagram.
怎么能实现这一目标?我没有使用故事板
func startUploadingImage() {
var localFileName:String?
// Issue #1: This here causes a segmentation fault 11 - Worked completely fine in swift 2.3
if let imageToUploadUrl = selectedImageUrl {
let phResult = PHAsset.fetchAssets(withALAssetURLs: [imageToUploadUrl], options: nil)
localFileName = phResult.firstObject?.fileManager
}
if localFileName == nil {
return
}
// Configure AWS Cognito Credentials
let myIdentityPoolId = ""
let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType:AWSRegionType.euWest1, identityPoolId: myIdentityPoolId)
let configuration = AWSServiceConfiguration(region:AWSRegionType.euWest1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
// Set up AWS Transfer Manager Request
let S3BucketName = "" …Run Code Online (Sandbox Code Playgroud) 所以我的 vc 中有一个 collectionview,如下所示:
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.headerReferenceSize = CGSize(width: self.view.frame.width, height: 96 + 42.5)
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.delegate = self
cv.dataSource = self
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
cv.alwaysBounceVertical = true
return cv
}()
Run Code Online (Sandbox Code Playgroud)
它有一个总高度为 138.5 像素的标题。所以显示单元格的集合视图的内容大小是view.frame.height - 138.5.
在我的 collectionview 中,我有一个高度为 176 像素的单元格,而view.frame.width宽度像这样:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 176)
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我返回 3 …
尝试用字符_替换空格
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.tag == 0 {
username = textField.text!
username = username.replacingOccurrences(of: " ", with: "_", options: .literal, range: nil)
textField.text! = username
}
return true
}
Run Code Online (Sandbox Code Playgroud)
麻烦的是,键入空格后的字符后,空格被替换了。
我tableView cellForRowAtIndexPath看起来像这样:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CheckoutAppointmentCell.reuseIdentifier) as! CheckoutAppointmentCell
cell.appointment = appointments[indexPath.row]
cell.checkoutButton.tag = indexPath.row
cell.checkoutButton.addTarget(self, action: #selector(checkoutButtonTapped), for: .touchUpInside)
return cell
}
Run Code Online (Sandbox Code Playgroud)
然后我从删除的预定tableView和dataSource像这样:
func checkoutButtonTapped(sender: UIButton) {
appointments.remove(at: sender.tag)
print(sender.tag)
//self.tableView.beginUpdates()
self.tableView.deleteRows(at: [IndexPath(row:sender.tag, section: 0)], with: .automatic)
//self.tableView.endUpdates()
}
Run Code Online (Sandbox Code Playgroud)
我第一次删除约会,它工作正常.该sender.tag值应该是它应该是正确的行从中删除tableView.
删除第一行后,似乎删除了不正确的行.
我在调用reloadData()后尝试调用deleteRows但动画不再出现.beginUpdates()并且endUpdates()似乎没有什么区别都不是.