小编Zou*_*ssi的帖子

void函数Swift3中出现意外的非void返回值

我有一个返回类对象或nil的函数.该函数的目的是检查是否Chat存在.聊天ID存储在MySQL中.如果ID存在,我执行Firebase引用以获取快照,然后获取该对象.如果ID不存在,我返回nil:

func findChat(string: String) -> Chat? {

    var returnValue: (Chat?)
    let url = getChatsURL
    let Parameters = [ "title" : string ]

    Alamofire.request("\(url)", method: .post, parameters: Parameters).validate().responseString { response in
            if let anyResponse = response.result.value {
                self.responseFromServer = anyResponse
            }

            if self.responseFromServer == "" {
              returnValue = nil
            } else {
                let ref = DatabaseReference.chats.reference()
                let query = ref.queryOrdered(byChild: "uid").queryEqual(toValue: (self.responseFromServer))
                query.observe(.childAdded, with: { snapshot in
                returnValue = Chat(dictionary: snapshot.value as! [String : Any])
            })
        }
        return returnValue …
Run Code Online (Sandbox Code Playgroud)

ios swift

3
推荐指数
1
解决办法
4047
查看次数

SwiftUI:如何“滑动删除”嵌套 ForEach 中的元素(分组)

我正在尝试在 SwiftUI 中创建一个动态分组列表,我想知道如何onDelete在这种情况下实现一个。从我读到的关于这个方法的内容来看,它采取了一个接收IndexSet的动作。当您只有一个 ForEach(无分组)时,这一切都很好,但是当您添加嵌套的 ForEach(实施分组)以实现“滑动删除”项目功能时,这一切都很好。问题是我不确定onDelete应该放在外部ForEach还是内部,两者都不起作用,因为无法检测传递中的哪个Category对象,因此我可以执行删除项目。有什么想法吗?这是我的模型和视图:itemsIndexSet

模型:

class Product: Identifiable, ObservableObject {
    let id = UUID()
    var name: String

    init(name: String) {
        self.name = name
    }
}

class Category: Identifiable, ObservableObject {
    let id = UUID()
    @Published var products = [Product]()
    var categoryName = ""
}

class Categories: ObservableObject {
    @Published var items = [Category]()
}
Run Code Online (Sandbox Code Playgroud)

并查看:

struct ProductListView: View {
    @ObservedObject var categories: Categories …
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

3
推荐指数
1
解决办法
2717
查看次数

如何在 SwiftUI 中实现触发 switch case 的左或右 DragGesture()?

我在视图中创建了一个 DragGesture,无论用户向左滑动还是向右滑动,它都应该选择一个 @State(Bool)。

问题是只检测到向右滑动。

如何使用 .gesture() 来捕捉用户是在他的屏幕上向左还是向右滑动?

import SwiftUI

struct SwiftUIView: View {

  //MARK: Value to change on swipe gesture
  @State var swipeRight: Bool

  var body: some View {
    VStack {
      //MARK: Value displayed after user swiped
      Text($swipeRight ? "Right" : "Left")
    }
    .gesture(
      DragGesture()
        .onChanged { (value) in
          //MARK: What is it missing here?
          switch value.location.x {
          case ...(-0.5):
            self.swipeRight = false
            print("Swipe Left return false")
          case 0.5...:
            self.swipeRight = true
            print("Swipe Right return true")
          default: ()
          }
    }) …
Run Code Online (Sandbox Code Playgroud)

gesture ios swift swiftui

3
推荐指数
3
解决办法
2707
查看次数

如何在 WidgetKit、SwiftUI 中更改午夜视图?

我有一个代码:

struct ContentView: View {
    let entry: LessonWidgetEntry
    private static let url: URL = URL(string: "widgetUrl")!
    var body: some View {
        VStack {
            switch entry.state {
            case .none:
                ProgramNotStartedView()
            case .currentLesson(let lesson):
                LessonView(lesson: lesson, imageName: entry.program?.imageName)
            case .lessonCompleted(let lesson):
                LessonCompletedView(lesson: lesson)
            case .programCompleted:
                ProgramCompletedView()
            }
        }.widgetURL(ContentView.url)
    }
}
Run Code Online (Sandbox Code Playgroud)

在午夜 LessonCompletedView 应该更改为 LessonView,但我不知道如何做到这一点。关于如何从小部件更改午夜视图的任何想法?

ios swift widgetkit swiftui

3
推荐指数
1
解决办法
418
查看次数

CollectionView 中用于评论和回复的多个单元格

我有两种不同的单元格类型,一种用于评论,另一种用于回复。我试图以相同的方式呈现它们collectionView,然后可能像这样对它们进行分组:每个具有特定 id 的评论下面都有其回复。然而,无论什么尝试,我都失败了。

你会怎样做呢?

private var comments = [Comment]()
private var replies = [Reply]()
var items: [Any] = []


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        let item = items[indexPath.item]

        var item = items[indexPath.item]

        if item is Comment.Type  {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CommentCell.cellId, for: indexPath) as! CommentCell
            cell.comment = items[indexPath.item] as? Comment
            print(item)
            return cell


        } else { …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview uicollectionviewcell swift

2
推荐指数
1
解决办法
1313
查看次数

如何将视图固定在屏幕边缘?

如何将视图(在这种情况下为标签/文本)固定到屏幕的边缘SwiftUI?使用情节提要,我只会使用,AutoLayout但无法使用SwiftUI

在此处输入图片说明

ios swift swiftui

1
推荐指数
1
解决办法
546
查看次数

即使 TraitCollection.userInterfaceStyle = Dark,也仅从 [UIColor colorNamed:] 返回浅色外观颜色

我正在努力将 iOS 13 深色模式支持添加到我的 iOS 11+ 应用程序中。在整个应用程序中使用命名/动态颜色效果很好。

#ffffff但是,当在自定义类中使用 [UIColor colorNamed:] 时,始终返回浅色版本( /白色)而不是深色版本( #000000/黑色):

// Some ViewController
CustomClass *custom = [customClass alloc] initWithTraitCollection:self.traitCollection]; 

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    custom.traitCollection = self.traitCollection;
}


// CustomClass
- (void)initWithTraitCollection:(UITraitCollection *)traitCollection {
    self = [super init];
    if (self) {
        self.traitCollection = traitCollection;
    }
    return self;
}

- (void)doSomething {    
    NSLog(@"CustomClass UIStyle: %@", (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? @"dark" : (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight ? @"light" : @"unspecified")));

    // Trying to get the color in …
Run Code Online (Sandbox Code Playgroud)

objective-c uicolor ios ios-darkmode

1
推荐指数
1
解决办法
1983
查看次数