dorentus
V2EX  ›  Swift

下班路上想到的一个 Swift 题目

  •  
  •   dorentus · Oct 10, 2015 · 3551 views
    This topic created in 3893 days ago, the information mentioned may be changed or developed.

    有一个class BrokenPrinter {},使用extension扩展它使支持这样的调用BrokenPrinter.print(),且满足:

    1. 第一次调用BrokenPrinter.print()的时候使用 Swift 的print()打印出字符串Hello, World.
    2. 之后每次调用BrokenPrinter.print(),都不再有任何打印。

    额外要求:
    1. 不要借助 Objective-C runtime
    2. 不要使用 GCD


    其实是下午写了类似的代码,然后下班路上忽然想到可以做成上面这样一道题

    明天给出我自己的答案,在之前我想看看大家的(如果有兴趣的话)各种解答。

    Supplement 1  ·  Oct 10, 2015
    3. 不借助外部变量
    Supplement 2  ·  Oct 12, 2015
    2 replies    2015-10-11 08:45:22 +08:00
    loveuqian
        1
    loveuqian  
       Oct 10, 2015 via iPhone   ❤️ 1
    不会 swift
    类似单例?
    baiyuxiong
        2
    baiyuxiong  
       Oct 11, 2015 via iPhone
    lazy load?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 19:27 · PVG 03:27 · LAX 12:27 · JFK 15:27
    ♥ Do have faith in what you're doing.