iOS 面试题 (一)

// 简易Demo
final class LSNNotificationCenter {
    private var dict: [String: [String: (String) -> Void]] = [:]
    static let defaultCenter = LSNNotificationCenter()
    
    private init() {}
    
    func addObserver(notification: String, id: String, block: @escaping (String) -> Void ) -> Void {
        if var blockDict = dict[notification] {
            blockDict.updateValue(block,forKey:id)
        } else {
            dict.updateValue([id:block], forKey: notification)
        }
    }
    
    func removeAllObserver(notification: String) -> Void  {
        dict.removeValue(forKey: notification)
    }
    
    func removeObserver(notification: String, id: String) -> Void {
        if var blockDict = dict[notification] {
            blockDict.removeValue(forKey: id)
        }
    }
    
    func postNotification(notification: String) -> Void {
        if let blockDict = dict[notification] {
            blockDict.forEach { (arg) in
                let (_, block) = arg
                block(notification)
            }
        }
    }
}


  • What's the difference between an IP address and a MAC address?

MAC address: Media Access Control Address, 直译为媒体访问控制地址,也称为局域网地址(LAN Address),以太网地址(Ethernet Address)或物理地址(Physical Address)。它是一个用来确认网络设备位置的地址。在OSI模型中,第三层网络层负责IP地址,第二层数据链接层则负责MAC地址。MAC地址用于在网络中唯一标示一个网卡,一台设备若有一或多个网卡,则每个网卡都需要并会有一个唯一的MAC地址。

IP地址(英语:IP Address, 全称:Internet Protocol Address),又译为网际协议地址、互联网协议地址。当设备连接网络,设备将被分配一个IP地址,用作标识。通过IP地址,设备间可以互相通讯,如果没有IP地址,我们将无法知道哪个设备是发送方,无法知道哪个是接收方。[2] IP地址有两个主要功能:标识设备或网络寻址(英语:location addressing)

常见的IP地址分为 IPv4IPv6 两大类,IP地址由一串数字组成。IPv4 由十进制数字组成,并以点分隔,如:172.16.254.1[3] IPv6 由十六进制数字组成,以冒号分割,如:2001:db8:0:1234:0:567:8:1[4]


  • What is CoreDataManager in iOS?
  1. iOS-CoreData详解与使用
  2. What Is Core Data?

  • What's the difference between using GCD & NSOperationQueue?

GCD: GCD is a low-level C-based API that enables very simple use of a task-based concurrency model.

NSOperationQueue: NSOperationQueue (and friends) are Objective-C classes that is internally implemented using GCD.

In general, you should use the highest level of abstraction that suits your needs. This means that you should usually use NSOperationQueue instead of GCD, unless you need to do something that NSOperationQueue doesn't support.

Caveat: On the other hand, if you really just need to send off a block, and don't need any of the additional functionality that NSOperationQueue provides, there's nothing wrong with using GCD. Just be sure it's the right tool for the job.


  • What is difference between class and struct?

Swift中 struct 和 class 的区别

类和结构体的不同点:

a、类具有:继承、类型转换、析构、引用计数
b、类和结构体在内存中的实现机制的不同:类存储在堆(heap)中,结构体存储在栈(stack)中
c、类是引用类型,而结构体是值类型

Classes have additional capabilities that structures don’t have:

a、Inheritance enables one class to inherit the characteristics of another.
b、Type casting enables you to check and interpret the type of a class instance at runtime.
c、Deinitializers enable an instance of a class to free up any resources it has assigned.
d、Reference counting allows more than one reference to a class instance.


  • Any experience in using monitoring tools for performance / crashes report?

Xcode
HockeyApp
Firebase
友盟umeng



  • How to reverse a doubly linked list with O(N) space complexity?
    reverse() {
        // if (this.length == 0 || this.length == 1) { return; }
        let cur = this.head;
        while (cur) {
            let prev = cur.prev;
            let next = cur.next;
            if (prev == null) { this.tail = cur; }
            else if (next == null) { this.head = cur; }
            cur.next = prev;
            cur.prev = next;
            cur = next;
        }
    }

  • How to sort a single string without using any api/library functions with O(N) time complexity?

** 桶排序 **

let set = { 'a': 1, 'b': 2, 'c': 3 };
let str = 'abbccc';

  • How to flatten this array [[4,5,6],3,2,1,[7,8]]
/**
 * @param {[]} arr
 * @return {[]} 
 */

// helper:  Array.isArray(value)
var flatten_array = function (arr) {
    let res = [];
    var helper = array => {
        for (let i = 0; i < array.length; i++) {
            const e = array[I];
            if (Array.isArray(e)) {
                helper(e);
            } else {
                res.push(e);
            }
        }
    }
    helper(arr);
    return res;
}

let input = [[4, 5, [6, 9]], 3, 2, 1, [7, 8]];
let x = flatten_array(input);
console.log(x);

// [4 5 6 9 3 2 1 7 8];

  • What is chained responsibility pattern?

职责链模式(Chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
🌰:Response Chain 响应链


const reverseWords = s => s.trim().split(/\s+/).reverse().join(' ');

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
    if (!nums) return [];
    let res = [];
    var permuteHelper = (outArr, inArr) => {
        if (outArr.length == nums.length) {
            res.push(outArr) 
            return;
        }
        for (let i = 0; i < inArr.length; i++) {
            let withoutI = inArr.slice(0, i).concat(inArr.slice(i + 1, inArr.length));
            permuteHelper(outArr.concat([inArr[i]]), withoutI)
        }
    }
    permuteHelper([], nums)
    return res;
};

  • given a middle pointer alone in a single linked list, how would u delete it.

A simple solution is to traverse the linked list until you find the node you want to delete. But this solution requires pointer to the head node which contradicts the problem statement.

Fast solution is to copy the data from the next node to the node to be deleted and delete the next node. Something like following.


  • Explain OSI models :


    2112694-8e11667011285470.png

  • MVVM
MVVM

  • What is the difference between "display:none" and "visibility:hidden"

display:none means that the tag in question will not appear on the page at all (although you can still interact with it through the dom). There will be no space allocated for it between the other tags.

visibility:hidden means that unlike display:none, the tag is not visible, but space is allocated for it on the page. The tag is rendered, it just isn't seen on the page.


  • What library for Javascript have you been working with?
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,904评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,581评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,527评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,463评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,546评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,572评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,582评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,330评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,776评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,087评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,257评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,923评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,571评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,192评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,436评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,145评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,127评论 2 352