我想迭代一个数组数组,以便搜索特定的项目,如果退出则返回true.
var fruits = ["apple", "banana"]
var names = ["ivan", "john", "maria"]
var mainArray = [fruits, names]
// i want to return true if theres a name/fruit that is "john"
func search() -> Bool {
for object in mainArray {
if (object.filter { $0 == "john" }).count > 0 {
return true
}
}
return false
}
search()
Run Code Online (Sandbox Code Playgroud)
这有效,但有一个较短的版本使用.map并避免在mainArray中的对象?像mainArray.map.filter ...?
是否可以像使用一样在单独的 var 中提取工具栏内容@ViewBuilder?
我想提取它并设置 .toolBar(content: myToolBarContent)
var body: some View {
NavigationView {
List {
}
.toolbar(content: {
ToolbarItem(placement: .principal) {
Text("Hi")
}
ToolbarItem(placement: .navigationBarTrailing) {
Text("Ho")
}
})
}
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
@implementation MyScene {
SKAction *delayAction;
}
Inside a method:
delayAction = [SKAction waitForDuration:3.0];
[self runAction:[SKAction repeatActionForever: [SKAction sequence:
@[delayAction, [SKAction ...]]]]]
withKey:@"myKey"];
Run Code Online (Sandbox Code Playgroud)
然后我想减少加班时间.(此方法在更新时调用:)所以我试过:
- (void)updateVelocity
{
NSLog(@"duration:%f",delayAction.duration);
delayAction.duration = delayAction.duration - 0.001;
}
Run Code Online (Sandbox Code Playgroud)
我得到:
2014-04-04 11:45:05.781 FlyFish[5409:60b] duration:1.300000
2014-04-04 11:45:05.785 FlyFish[5409:60b] duration:1.299000
2014-04-04 11:45:05.800 FlyFish[5409:60b] duration:1.298000
2014-04-04 11:45:05.816 FlyFish[5409:60b] duration:1.297000
Run Code Online (Sandbox Code Playgroud)
这似乎很好,但我的[SKAction ...]仍然在3秒后继续重复.
我以编程方式创建视图,但无法开始setContentHuggingPriority工作。这是当前结果:
private lazy var stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
[button, text].forEach(stackView.addArrangedSubview)
return stackView
}()
private lazy var button: UIButton = {
let button = UIButton()
button.setImage(Asset.configIcon.image, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setContentHuggingPriority(.defaultHigh, for: .horizontal)
button.backgroundColor = .red
return button
}()
private lazy var text: UILabel = {
let label = UILabel()
label.text = "This line should have more width than the button"
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.setContentHuggingPriority(.defaultLow, for: .horizontal)
return …Run Code Online (Sandbox Code Playgroud) 当我在订阅之前添加toArray()时,我得不到回调.
googleCalendarUseCase.getEventsFromCalendars(calendars: selectedCalendars).subscribe(onNext: { (event) in
print(event.summary) //print thousands of elements
}).addDisposableTo(disposeBag)
googleCalendarUseCase.getEventsFromCalendars(calendars: selectedCalendars).toArray().subscribe(onNext: { (events) in
print(events.count) // Never gets called
}).addDisposableTo(disposeBag)
Run Code Online (Sandbox Code Playgroud)
也许问题是函数getEventsFromCalendar但不确定为什么它工作如果我不做toArray():
func getEventsFromCalendars(calendars: [GoogleCalendar.Calendar], nextPageToken: String? = nil) -> Observable<GoogleCalendar.Event> {
return Observable<GoogleCalendar.Event>.create { observer -> Disposable in
var parameters: [String: Any] = [:]
if let nextPageToken = nextPageToken {
parameters["pageToken"] = nextPageToken
}
_ = self.oauthswift.client.get(GoogleCalendarAPI.events, parameters: parameters, success: { (data, response) in
if let json = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] { …Run Code Online (Sandbox Code Playgroud) 我给 UIViewController 添加了一个扩展来添加一个关闭按钮
extension UIViewController {
func addCloseButton() {
let button = UIBarButtonItem(image: #imageLiteral(resourceName: "bar_close"),
landscapeImagePhone: nil,
style: .done,
target: self,
action: #selector(UIViewController.dismiss(animated:completion:)))
navigationItem.leftBarButtonItem = button
}
}
Run Code Online (Sandbox Code Playgroud)
当我点击栏按钮时,我会直接崩溃到 AppDelegate。任何提示?似乎与选择器有关。
ios ×6
swift ×6
autolayout ×1
iphone ×1
objective-c ×1
rx-swift ×1
skaction ×1
sprite-kit ×1
swiftui ×1