不会在“用户结构”上创建“ currentUser类型属性”;导致无限递归? [英] Doesn't creating "a currentUser type property on a User struct" lead to infinite recursion?

查看:64
本文介绍了不会在“用户结构”上创建“ currentUser类型属性”;导致无限递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Apple的使用Swift开发应用程序学习Swift编程。
我在第2.3章中的以下任务遇到了困难:

Learning Swift programming with "App development with Swift" from Apple. I've faced a difficulty with the following task in Chapter 2.3:


假设您有一个需要用户执行以下操作的应用程序登录。您可能具有类似于下面所示的
a User 结构。但是,除了
跟踪特定的用户信息外,您可能还希望以
的方式知道当前登录用户是谁。在下面的 User 结构上创建 currentUser
类型的属性,并将其分配给用户对象
代表您。现在,您可以通过 User
结构访问当前用户。打印出 currentUser 的属性。

Imagine you have an app that requires the user to log in. You may have a User struct similar to that shown below. However, in addition to keeping track of specific user information, you might want to have a way of knowing who the current logged in user is. Create a currentUser type property on the User struct below and assign it to a user object representing you. Now you can access the current user through the User struct. Print out the properties of currentUser.

struct User {

    var userName: String

    var email: String

    var age: Int

}


如果我理解正确,则需要创建 currentUser User 结构中类型为 User 的c>属性?这将导致无限递归,不是吗?因此,我不确定我是否正确。

If I understood correctly, I need to create currentUser property of type User within the User struct? This will lead to an infinite recursion, won't it? Hence, I'm not sure that I got this correct. Please help to clarify.

推荐答案

请仔细阅读练习。它要求您创建一个 类型属性 ,它与您之前创建的实例属性不同。也就是说,您正在寻找一种方法来询问 User 一般类型的当前登录用户是谁,而不是询问某些特定用户。 (由于这是一个学习练习,我会停在这里,您可能想自己找到一个完整的解决方案...)

Read the exercise carefully. It's asking you to create a type property, which is not the same as the instance properties you've created before. That is, you're looking for a way to ask the User type in general who the currently logged in user is, not ask some specific user. (I'll stop there since this is a learning exercise and you probably want to reach a complete solution on your own...)

关于递归问题:完全是一个类型可以引用该类型的实例,或者一个类型的实例可以引用其他实例。例如,您可以在 User 上创建一个 favoriteTeacher 属性,该属性指向的另一个实例。用户

Regarding the recursion question: It's totally okay for a type to refer to instances of that type, or for instances of a type to refer to other instances. You could, for example, create a favoriteTeacher property on User that points to another instance of User.

您需要注意的是您是否要创建圆形结构(例如,如果我说我最喜欢的老师是@nate-cook ,内特说他最喜欢的老师是 @ matt ,Matt说他最喜欢的老师是我),以及您编写的算法是否可以安全地处理圆形结构而不会陷入无限递归或无限循环中。 (如果您打算通过跟踪 favoriteTeacher 属性来查找所有喜欢的老师,直到找到一个 nil 的属性,则可以会很难过的。)

What you do have to watch out for is whether you're creating circular structures (for example, if I say my favorite teacher is @nate-cook, Nate says his favorite teacher is @matt, and Matt says his favorite teacher is me), and whether the algorithm you're writing can safely process a circular structure without getting stuck in infinite recursion or an infinite loop. (If you're planning to find all the favorite teachers by following favoriteTeacher properties until you find one that's nil, you're gonna have a bad time.)

这篇关于不会在“用户结构”上创建“ currentUser类型属性”;导致无限递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆