我正在尝试设置iOS 8自动调整大小单元UICollectionViewController:
我使用StoryBoards创建了一个基本设计,并设计了一个UICollectionViewCell,其标签约束到单元格的边界.然后,我将该单元格的标签链接到Cell的类,以便能够以编程方式访问它.
没有指定estimatedItemSize,一切正常(当然除了自动调整大小).
当我指定estimatedItemSize这种方式时:
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
layout.estimatedItemSize = CGSizeMake(100.0f, 30.0f);
}
Run Code Online (Sandbox Code Playgroud)
应用程序崩溃时出现以下错误消息:
the behavior of the UICollectionViewFlowLayout is not defined because:
the item width must be less than the width of the UICollectionView minus the section insets left and right values.
Please check the values return by the delegate.
Run Code Online (Sandbox Code Playgroud)
我想,我estimatedItemSize在错误的地方设置,或者,如果我已经在故事板中设置了指定的项目大小,那么它与估计的大小有某种冲突.
我在iOS应用中使用密码自动填充功能。
我分别将所需的textContentType属性设置为username和password。另外,我在iOS设置中启用了iCloud钥匙串。
随即出现该栏,我可以使用其他应用程序的凭据填写登录表单。但是,我无法保存输入的凭据。
是否可以将凭据保存到钥匙串中而无需将应用程序与网站相关联(如WWDC会话中所示)?
我有两个协议:Pen和InstrumentForProfessional。我想使任何一支笔成为Professional的Instrument:
protocol Pen {
var title: String {get}
var color: UIColor {get}
}
protocol Watch {} // Also Instrument for professional
protocol Tiger {} // Not an instrument
protocol InstrumentForProfessional {
var title: String {get}
}
class ApplePen: Pen {
var title: String = "CodePen"
var color: UIColor = .blue
}
extension Pen: InstrumentForProfessional {} // Unable to make ApplePen an Instument for Professional: Extension of protocol Pen cannot have …Run Code Online (Sandbox Code Playgroud) 一个ViewModel类有一个sourceProperty正在由 编辑的TextField。该属性是@Published. 我想将它Logic传递给具有Binding<String>. 该类将监听更改sourceProperty,对其做出反应并将其输出设置为属性@Published output。
如何@Published sourceProperty作为初始化参数传递给类Logic?
相关代码:
final class ViewModel {
@Published var sourceProperty: String = ""
private var logic: Logic?
init() {
self.logic = Logic(data: $sourceProperty)
$logic.output.sink({result in
print("got result: \(result)")
})
}
}
final class Logic: ObservableObject {
private var bag = Set<AnyCancellable>()
@Published var output: String = ""
@Binding var data: String
init(data: Binding<String>) {
self._data …Run Code Online (Sandbox Code Playgroud) 基于这个答案:What is the Difference Between ObservedObject and StateObject in SwiftUI
\n苹果文档代码在这里:https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app
\n在 SwiftUI 应用程序中,实例化对象本身@StateObject时应使用属性包装器View,以便在视图更新期间不会重新创建该对象。
如果该对象在其他地方实例化,@ObservedObject则应使用包装器。
然而,有一条细线使它有点不清楚:如果该对象在其他地方实例化,但“注入”到该View对象,然后该视图是该对象的唯一所有者/持有者,该怎么办?应该是@StateObject还是@ObservedObject?
示例代码来说明这一点:
\nimport SwiftUI\nimport Combine\nimport Foundation\n\n\nstruct ViewFactory {\n func makeView() -> some View {\n let viewModel = ViewModel()\n return NameView(viewModel)\n }\n}\n\n\nfinal class ViewModel: ObservableObject {\n @Published var name = ""\n init() {}\n}\n\n\nstruct NameView: View {\n\n // Should this be an `@ObservedObject` or `@StateObject`?\n @ObservedObject var …Run Code Online (Sandbox Code Playgroud) UIKit 中的类似问题:How to dismiss UIAlertController when tap Outside the UIAlertController?
我有一个带有几个按钮的基本警报(取消/删除样式)。我想通过点击警报外部来“关闭”警报,类似于“取消”按钮的操作:
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button("Show Alert") {
showingAlert = true
}
.alert("Important message", isPresented: $showingAlert) {
Button("First") { }
Button("Second") { }
Button("Third") { }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试添加onTapGesture处理程序,但没有帮助。
我在库中有以下 C 风格结构:
typedef struct ServiceParam
{
const char *name;
const char *value;
} ServiceParam;
Run Code Online (Sandbox Code Playgroud)
我有兴趣从 Swift 初始化这些结构的数组,这是我尝试过的:
let cHeaders = headers.map{ServiceParam(name: $0.name, value: $0.value)}
Run Code Online (Sandbox Code Playgroud)
但收到以下警告:
将“String”传递给参数,但参数“value”应该是一个比“init(name:value:)”调用更长久的指针
和C 参数以name的value形式进行转换UnsafePointer<CChar>!,输入类型为(name: String, value: String),即 Swift-tuple,但我可以灵活地更改初始类型。
因此,整个最小示例如下所示:
public func setParams(headers: [(name: String, value: String)] = []) {
let cHeaders = headers.map{ServiceParam(name: $0.name, value: $0.value)}
// Do the work with `cHeaders`
}
Run Code Online (Sandbox Code Playgroud)
从 Swift 调用站点初始化上述 C 风格结构的最佳方法是什么?
该ServiceParam结构仅在父函数调用期间临时使用,但name和value字符串作为 C++ 对存储在数组中,并且它们的生命周期在函数返回后继续: …
代码:
import SwiftUI
public struct Snackbar<Content>: View where Content: View {
private var content: Content
// Works OK
public init(@ViewBuilder content: () -> Content) {
self.content = content()
}
init(_ text: String) {
self.init {
Text(text) // cannot convert value of type 'Text' to closure result type 'Content'
.font(.subheadline)
.foregroundColor(.white)
.multilineTextAlignment(.leading)
}
}
public var body: some View {
HStack {
VStack(alignment: .leading, spacing: 4) {
content
}
Spacer()
}
.frame(maxWidth: .infinity,
minHeight: 26)
.padding(.fullPadding)
.background(Color.black)
.clipShape(RoundedRectangle(cornerRadius: .defaultCornerRadius))
.shadow(color: Color.black.opacity(0.125), …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 …
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
swiftui ×4
combine ×2
objective-c ×2
alert ×1
arrays ×1
autofill ×1
autolayout ×1
autosize ×1
binding ×1
c ×1
codable ×1
data-binding ×1
decodable ×1
icloud ×1
import ×1
initializer ×1
json ×1
keychain ×1
login ×1
pointers ×1
protocols ×1
publisher ×1
string ×1
viewbuilder ×1
xcode ×1