我正在使用 Swift 4 和JSONDecoder. 我有以下结构:
struct Customer: Codable {
var id: Int!
var cnum: String!
var cname: String!
}
Run Code Online (Sandbox Code Playgroud)
注意:这些字段不能是可选的。
现在我有一个 JSON 字符串:
[
{
"id": 1,
"cnum": "200",
"cname": "Bob Smith"
},
{
"id": 2,
"cnum": "201",
"cname": null
}
]
Run Code Online (Sandbox Code Playgroud)
为了对其进行解码,我使用以下命令:
let decoder = JSONDecoder()
let customers = try decoder.decode([Customer].self, from: json)
Run Code Online (Sandbox Code Playgroud)
除了空数据被转换为 nil 之外,一切正常。我的问题是,将传入的 nil 转换为空字符串(“”)的最简单方法是什么?
我想用最少的代码来做到这一点,但我不确定正确的方法以及在什么时候可以将 nil 转换为空字符串。预先谢谢你。
我试图更改导航控制器后退按钮的字体大小,但无济于事。
我目前正在以前的视图控制器中执行此操作viewWillAppear:
navigationController?.navigationBar.backItem?.backBarButtonItem?.setTitleTextAttributes(
[NSAttributedStringKey.font : UIFont.systemFont(ofSize: 5)], for: .normal)
Run Code Online (Sandbox Code Playgroud)
上面的代码什么也没做。所以,有两个问题。
首先,更改后退按钮字体大小的正确方法是什么(顺便说一句,这似乎比应有的要困难得多)。
其次,是否有一个单一的地方(如 AppDelegate)我可以进行此更改,以便它影响所有视图控制器?
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == self.collectionViewVideo {
var collectionViewSize = collectionViewVideo.frame.size
collectionViewSize.width = collectionViewSize.width/3.0 //Display Three elements in a row.
return collectionViewSize
} else {
return CGSize(width: 60, height: 60)
}
}
Run Code Online (Sandbox Code Playgroud)
ios uicollectionview swift swift4 uicollectionviewflowlayout
我正在尝试添加删除按钮作为图像中的子视图。这是我当前的结构:
-> class DesignViewController: UIViewController
|
-> class Sticker: UIImageView, UIGestureRecognizerDelegate
|
-> UI button inside the Sticker
在 Sticker 类中我有:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let button2 = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button2.backgroundColor = .red
button2.setTitle("Delete", for: .normal)
button2.tag = 23
button2.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.addSubview(button2)
}
@objc func buttonAction(sender: UIButton!) {
print("Button tapped")
}
Run Code Online (Sandbox Code Playgroud)
没有buttonAction被调用。当我将self.addSubview(button2)线路更改为:
self.superview?.addSubview(button2)
Run Code Online (Sandbox Code Playgroud)
我可以看到buttonAction有人打电话。不过,我想将按钮保留在贴纸视图内,以便当用户移动贴纸时,按钮会作为子视图随之移动。
谁能帮忙并让我知道如何将按钮保留在贴纸视图中?
在 swift 4 中,您可以使用以下方法提取子字符串:
let str = "abcdefghci"
let index = str.index(of: "c") ?? str.endIndex
print(str[...index])
Run Code Online (Sandbox Code Playgroud)
这将打印 abc
但是我怎样才能找到最后一个的索引,c从那个位置提取一个子字符串呢?
更新
@vadian 请看附图:
我有 MKMarkerAnnotationView 来更改地图上图钉的颜色。
func mapView(_ MapView:MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
view.markerTintColor = .blue
return view
}
Run Code Online (Sandbox Code Playgroud)
但是,当我启动我的应用程序时,我的默认位置标记会更改为。如何在不更改此标记的情况下更改引脚?查看位置的代码也很简单
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
self.MapView.showsUserLocation = true
}
Run Code Online (Sandbox Code Playgroud)
感谢您的回答!:)
我希望能够查看用户是否位于 Swift 4 中其他位置的 5 公里半径范围内。
例如:
User Location: 37.785834, -122.406417
Other Location: -117.564011, 48.302353
Run Code Online (Sandbox Code Playgroud)
感谢任何提供帮助的人!
我正在 Swift 4.x 中创建一个新项目,我正在为我的项目寻找一些最佳解决方案。我想知道我们应该如何管理公共消息、JSON 密钥、网络调用、公共常量等……请参阅下面的一些示例:
我将在整个应用程序中使用的常见消息:
我有来自服务器的 JSON 密钥:
我有 200 多个服务器 API:
我有几个常数:
所以我的问题是我应该把所有这些东西放在哪里?
我应该创建单独的文件并在需要的地方导入吗?
我应该在文件本身需要的地方使用所有这些吗?但是随后会出现重复的问题,例如,我需要两个不同文件中的 URL 或屏幕大小。
我应该把所有这些都放在 plist 中吗?但是随后可能会出现 I/O 问题(不确定)?
我应该将所有这些放在应用程序委托文件中吗?
我创建了自己的 pod,其中包含 s.version = "0.4.7" 的 podspec 文件,我希望以编程方式将其写入代码,因此每当应用程序运行时,它都会将 pod 版本发送到服务器。
获取 pod 版本的另一个地方是从下面的 plist 文件,即“捆绑版本字符串”0.4.7
我试过使用,let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String但它从 Apps Info.plist 文件中获取版本。
我设置了我的约束,以便在您加载时有一个占据屏幕的图表,但您可以向下滚动以查看更多信息。但是,我可以看到滚动条在移动,而图表没有移动!
var scrollView: UIScrollView {
let scroll = UIScrollView()
self.view.addSubview(scroll)
scroll.snp.makeConstraints { (make) in
make.edges.equalTo(self.view)
}
return scroll
}
var contentView: UIView {
let content = UIView()
self.scrollView.addSubview(content)
content.snp.makeConstraints { (make) in
make.top.bottom.equalTo(self.scrollView)
make.left.right.equalTo(self.view)
make.width.equalTo(self.scrollView)
}
// self.scrollView.contentSize = content.frame.size
return content
}
override func viewDidLoad() {
super.viewDidLoad()
self.contentView.addSubview(self.chart)
self.chart.snp.makeConstraints { (make) in
// http://snapkit.io/docs/
// https://github.com/SnapKit/SnapKit/issues/448
make.top.equalTo(self.contentView)
make.left.right.equalTo(self.contentView)
make.height.equalTo(self.view).offset(-(self.tabBarController?.tabBar.frame.height)!)
}
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: 3000) // Testing
}
Run Code Online (Sandbox Code Playgroud)
swift4 ×10
swift ×8
ios ×5
annotations ×1
autolayout ×1
cllocation ×1
cocoapods ×1
jsondecoder ×1
mapkit ×1
plist ×1
snapkit ×1
uibutton ×1
uiimageview ×1
uiscrollview ×1
xcode ×1