我的目标是显示历史登录的用户列表(例如用户名)(如果有).为了做到这一点,我正在做
1. Create an custom object named User like below
class User: NSObject
{
var login: String
init(login: String)
{
self.login = login
}
required init(coder aDecoder: NSCoder) {
login = aDecoder.decodeObjectForKey("login") as! String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(login, forKey: "login")
}
}
// This conform to make sure that I compare the `login` of 2 Users
func ==(lhs: User, rhs: User) -> Bool
{
return lhs.login == rhs.login
}
Run Code Online (Sandbox Code Playgroud)
在的UserManager,Im做save和retrieve的User.在保存之前,我正在检查历史记录登录列表是否包含a User,我不会添加它,否则. …
以下代码在Swift 3中编译
extension Array where Element: Equatable {
var removeDuplicate: [Element] {
return reduce([]){ $0.0.contains($0.1) ? $0.0 : $0.0 + [$0.1] }
}
}
Run Code Online (Sandbox Code Playgroud)
但会产生错误
错误:上下文闭包类型'(_,_) - > _'需要2个参数,但在闭包体中使用了1
在Swift 4.如何转换此代码以在Swift 4中编译?
我一直在对我的模型进行一些测试,以确保当我将它们编码为 JSON 然后使用JSONEncoder/Decoder. 然而,我的一项测试失败了,罪魁祸首是UIImage。我已确保在编码/解码过程中没有抛出任何错误。
首先,这是有问题的测试:
func testProfileImageCodable() throws {
let image = ProfileImage(UIImage(systemName: "applelogo")!)
try XCTAssertTrue(assertCodable(image))
}
Run Code Online (Sandbox Code Playgroud)
这是我的“可编码性”测试,我确保编码/解码之前和之后类型相等:
func assertCodable<T: Codable & Equatable>(
_ value: T,
decoder: JSONDecoder = .init(),
encoder: JSONEncoder = .init()
) throws -> Bool {
let encoded = try encoder.encode(value)
let decoded = try decoder.decode(T.self, from: encoded)
return value == decoded
}
Run Code Online (Sandbox Code Playgroud)
首先,这是我的UIImage工作方式Codable:
extension KeyedEncodingContainer {
mutating func encode(_ value: UIImage, forKey key: Key) throws {
guard let …Run Code Online (Sandbox Code Playgroud) 我有一个具有键控属性的对象:
func value(key: String) -> AnyObject?
func setValue(value: AnyObject?, key: String)
Run Code Online (Sandbox Code Playgroud)
我想检查value从两个不同对象的具有相同键的函数返回的值是否为Equatable. Swift 抱怨是因为Equatable有一个Self引用并且只能在泛型中使用。有没有办法可以检查AnyObject从value函数返回的对象是否符合Equatable?
在回答关于 SO 的另一个问题时,我发现CLLocation该类符合Equatable协议。它使用什么方法来确定相等性?
纬度/经度的精确匹配?纬度/经度和高度的精确匹配?纬度、经度、高度和时间戳的精确匹配?速度和路线呢?什么CLLocation是单纯用纬度/经度对创建的对象?位置的各种其他值不是可选的,那么使用创建的位置的海拔高度是init(latitude:longitude:)多少?
我正在尝试为基于左操作数和右操作数标识的协议实现Equatable协议。换句话说:我如何为一个协议实现Equatable协议,以确定实现此协议的两个实例(在我的情况下iNetworkSubscriber)是否相同(相同的对象引用)。这样(错误消息包含在下面的代码中):
protocol iNetworkSubscriber : Equatable {
func onMessage(_ packet: NetworkPacket)
}
func ==(lhs: iNetworkSubscriber, rhs: iNetworkSubscriber) -> Bool { // <- Protocol 'iNetworkSubscriber' can only be used as a generic constraint because it has Self or associated type requirements
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs) // <- Cannot invoke initializer for type 'ObjectIdentifier' with an argument list of type '(iNetworkSubscriber)'
}
Run Code Online (Sandbox Code Playgroud)
......我也与身份运营商自己试了一下:
func ==(lhs: iNetworkSubscriber, rhs: iNetworkSubscriber) -> Bool { // <- Protocol 'iNetworkSubscriber' can only be used as a …Run Code Online (Sandbox Code Playgroud) Swift 文档表示,Equatable只有在结构体或枚举的原始定义中声明了一致性,而不是通过扩展声明一致性时,一致性的自动合成才可用于结构体和具有关联值的枚举,在这种情况下,运算符的实现==必须是假如。
但是,以下代码可以工作。
struct Point {
var x: Double
var y: Double
}
extension Point: Equatable {}
print(Point(x: 10, y: 10) == Point(x: 5, y: 5)) // prints false
Run Code Online (Sandbox Code Playgroud)
这也是如此。
enum Outcome {
case success
case failure(reason: String)
}
extension Outcome: Equatable {}
print(Outcome.failure(reason: "Chance") == Outcome.failure(reason: "Chance")) // prints true
Run Code Online (Sandbox Code Playgroud)
有谁知道这个功能在哪里记录。
谢谢。
试图获取数组的索引([AnyObject]).我错过了什么部分?
extension PageViewController : UIPageViewControllerDelegate {
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [AnyObject]) {
let controller: AnyObject? = pendingViewControllers.first as AnyObject?
self.nextIndex = self.viewControllers.indexOf(controller) as Int?
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用Swift 1.2这种方法:
func indexOf<U: Equatable>(object: U) -> Int? {
for (idx, objectToCompare) in enumerate(self) {
if let to = objectToCompare as? U {
if object == to {
return idx
}
}
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
嗨,我是 flutter 中的 bloc 新手,我正在尝试了解块计时器在 flutter_bloc 的文档中,我知道这个构造函数类是什么意思
@immutable
abstract class TimerState extends Equatable {
final int duration;
//and this list props ??
TimerState(this.duration, [List props = const []])
: super([duration]..addAll(props));
}
Run Code Online (Sandbox Code Playgroud) 我目前正在关注 resocoders clean 架构,在该架构中,他强烈建议使用Equatable来进行基本数据保存类,以便将来进行比较。问题是我的课程看起来像这样:
class Person extends Equatable {
final String name;
final List<AlterEgo> alterEgos;
Person({@required name, @required alterEgos}):super([name, alterEgos]);
@override
List<Object> get props => [name, alterEgos];
}
class AlterEgo extends Equatable {
final String name;
final String superPower;
AlterEgo({@required name, @required superPower}):super([name, superPower]);
@override
List<Object> get props => [name, superPower];
}
void main(){
Person("Clark", <AlterEgo>[AlterEgo("Superman", "Flying")]) == Person("Clark", <AlterEgo>[AlterEgo("Superman", "Flying")]) //true
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我编写构造函数时,IDE 会抱怨列表不可比较。我有点不知道现在要定义哪些函数/类来获取由原始类型组成的对象列表以与 equatable 一起使用。该包的文档似乎也省略了这个用例,仅声明 Equatable 仅适用于不可变类型。不过,我根本不介意该列表是不可变的。
编辑:完成的示例。目前在移动设备上,无法立即测试。