我UIStackView用来定义列表的布局。
我正在尝试实现与此类似的效果:
||Item1---spacing---Item2||
Run Code Online (Sandbox Code Playgroud)
因此,这两个项目被推到UIStackView.
但是,项目组合在一起的每一个可能的组合distribution和alignment:
||Item1-Item2-----spacing-----||
Run Code Online (Sandbox Code Playgroud)
我尝试设置一个大的间距值,即 500。在这种情况下,Item2只是离开屏幕。
有什么办法来“配合” Item1,并Item2到的左,右两侧UIStackView,同时允许间隔取决于屏幕宽度的变化?
我正在使用Autolayout和UICollectionView的AutoSizing单元格.
我可以在单元初始化的代码中指定约束:
func configureCell() {
snp.makeConstraints { (make) in
make.width.equalToSuperview()
}
}
Run Code Online (Sandbox Code Playgroud)
然而,应用程序崩溃,因为单元格尚未添加到collectionView.
问题
在cell生命周期的哪个阶段,可以用cell's width?添加约束?
有没有作出任何默认方式cell的widthequal to the
宽of the的CollectionView without accessing an instance of
UIScreen orUIWindow`?
编辑 问题不重复,因为它不是关于如何使用AutoSizing单元格功能,而是在单元生命周期的哪个阶段应用约束以在使用AutoLayout 时实现所需的结果.
Swift标准库中是否有作用于集合的函数,采用谓词并返回从该集合中删除的值?
目前,我必须分两步实施:
guard let idx = allAnnotations.index(where: {$0 is MKUserLocation}) else {return}
let userLocation = allAnnotations.remove(at: idx) as! MKUserLocation
Run Code Online (Sandbox Code Playgroud)
但是我想,存在类似的功能。
目标
我有以下数组:
[Type1, Type1, Type1, Type1, Type1, Type1, Type2]
Run Code Online (Sandbox Code Playgroud)
Type2数组中可能存在也可能不存在。除了这两种以外,没有其他类型。
我需要将其分为两个元素:
[Type1, Type1, Type1, Type1, Type1, Type1]
Run Code Online (Sandbox Code Playgroud)
和
Type2?
Run Code Online (Sandbox Code Playgroud)
那就是我要寻找的功能。
我有以下代码来提取编码密钥中包含的JSON:
let value = try! decoder.decode([String:Applmusic].self, from: $0["applmusic"])
Run Code Online (Sandbox Code Playgroud)
这成功处理了以下JSON:
{
"applmusic":{
"code":"AAPL",
"quality":"good",
"line":"She told me don't worry",
}
Run Code Online (Sandbox Code Playgroud)
但是,无法使用以下编码密钥提取JSON applmusic:
{
"applmusic":{
"code":"AAPL",
"quality":"good",
"line":"She told me don't worry",
},
"spotify":{
"differentcode":"SPOT",
"music_quality":"good",
"spotify_specific_code":"absent in apple"
},
"amazon":{
"amzncode":"SPOT",
"music_quality":"good",
"stanley":"absent in apple"
}
}
Run Code Online (Sandbox Code Playgroud)
数据模型applmusic,spotify并且amazon是不同的.但是,我只需要提取applmusic和省略其他编码密钥.
我的Swift数据模型如下:
public struct Applmusic: Codable {
public let code: String
public let quality: String
public let line: String
}
Run Code Online (Sandbox Code Playgroud)
API以完整的JSON响应,我不能要求它只给我所需的字段.
如何只解码json的特定部分?看起来,这Decodable …
什么是实现以下目的的正确URL格式:
什么不起作用:
let encoded = "https://www.google.com/maps/dir/?api=1&destination=-20.021999%2C57.579075"
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
另外,我尝试使用addingPercentEncoding方法对URL进行编码:
let string = "https://www.google.com/maps/dir/?api=1&destination=\(lat),\(long)"
let encoded = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
在两种情况下,结果都是相同的:“不支持的链接-Google Maps无法打开此链接”

另一方面,如果我删除Google Maps应用程序,或在模拟器中尝试相同的代码,则一切工作正常,并且可以完全达到我想要的结果:

为什么同一链接可以成功启动“ Web”应用程序,但是无法被本机应用程序识别?
有问题的字串:
https://www.google.com/maps/dir/?api=1&destination=-20.021999,57.579075
我要遵循的指南:Google Maps Guide
google-maps urlencode deep-linking swift ios-universal-links
在大多数教程或建议中,将60 fps的值列为该应用程序的最终刷新率。即,如果该应用程序能够始终如一地以60 fps的速度提供用户体验,则没有更多的改进空间。
但是,对于具有120 Hz刷新率的较新设备(例如iPad Pro),情况如何?他们是否真的每秒重绘UI组件120次,这意味着成功准备帧的时间从大约16毫秒减少到8毫秒?
概括:
我想创建一个其中Class<T>包含相应ClassDelegate协议的协议func<T>。
目标:
通过多个对象类重用单个对象和行为。接收已有专门类的委托回调,无需将对象转换为特定类即可使用它。
示例代码:
具有通用方法的协议:
protocol GenericTableControllerDelegate: AnyObject {
func controller<T>(controller: GenericTableController<T>, didSelect value: T)
}
Run Code Online (Sandbox Code Playgroud)
通用基子类UITableViewController:
open class GenericTableController<DataType>: UITableViewController {
weak var delegate: GenericTableControllerDelegate?
var data = [DataType]()
open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = data[indexPath.row]
delegate?.controller(controller: self, didSelect: item)
}
}
Run Code Online (Sandbox Code Playgroud)
的专门版本GenericTableController:
final class SpecializedTableController: GenericTableController<NSObject> {}
Run Code Online (Sandbox Code Playgroud)
-的客户端SpecializedTableController实现了结果,但需要类型转换才能访问专用数据类型:
final class ClientOfTableController: UIViewController, GenericTableControllerDelegate {
// Works OK
func …Run Code Online (Sandbox Code Playgroud) 根据技术问答 QA1561,不建议以任何方式退出应用程序。适当的方式是允许用户自行退出应用程序,而不是强制退出或中止应用程序的执行。
但是,Microsoft Teams iOS 应用程序以某种方式设法不仅避免了崩溃,而且以动画方式优雅地退出,类似于按下设备上的“主页”按钮。
请注意,在上述示例中没有按下主页按钮。该应用程序以编程方式执行动画退出
如何在您的设备上重现此行为:
我正在将作为 Xcode 项目构建和测试的库迁移到 Swift Package Manager。该项目包含 3 个目标:库本身、具有不同依赖关系的相同库以及测试(应用程序)目标。
到目前为止,移植库很容易。
现在我有兴趣构建具有不同依赖项(模拟)的相同库,可以对其进行测试。
实际上,Package.swift 文件如下所示:
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "TargetLibrary",
platforms: [
.iOS(.v14), .macOS(.v10_15)
],
products: [
.library(
name: "TargetLibrary",
targets: ["TargetLibrary"]
),
],
dependencies: [
.package(
url: "ssh://git@github.com/DependentLib",
.upToNextMinor(from: "1.3.18")
),
],
targets: [
.target(
name: "TargetLibrary",
dependencies: [
// Using the "main" dependency for the "TargetLibrary"
.product(name: "DependentLib", package: "DependentLib"),
],
path: …Run Code Online (Sandbox Code Playgroud) Swift 有一个有趣的关键字,可以在 中声明ModuleA:
@_exported import Foundation
Run Code Online (Sandbox Code Playgroud)
然后,当在其他模块(例如ModuleB)中时,我导入ModuleA:
import ModuleA
let currentDate = Date() // using a Foundation struct
Run Code Online (Sandbox Code Playgroud)
Foundation...即使我没有使用语句导入它,我也可以使用类型import。它会被自动导入,因为它已@_exported在ModuleA.
现在我想在 Objective-C 中有类似的行为。
鉴于:
TargetA(Swift)、ProtocolTarget(带有关键字注释类型的 Swift @objc) 和TargetB(ObjC)ProtocolTarget隐式导入,以便在TargetB导入时TargetA,来自 的所有方法ProtocolTarget都应该在TargetA.我怎样才能实现这个目标?
import objective-c swift swift-package-manager swift-package
ios ×7
swift ×7
autolayout ×2
uikit ×2
arrays ×1
autosize ×1
cocoa-touch ×1
codable ×1
collections ×1
decodable ×1
deep-linking ×1
dependencies ×1
generics ×1
google-maps ×1
home-button ×1
homescreen ×1
import ×1
iphone ×1
json ×1
objective-c ×1
performance ×1
protocols ×1
refresh ×1
rendering ×1
uistackview ×1
urlencode ×1