- Implement a class which is similar to NotificationCenter in iOS
YBNotification
透彻理解 NSNotificationCenter 通知
// 简易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地址分为 IPv4 与 IPv6 两大类,IP地址由一串数字组成。IPv4 由十进制数字组成,并以点分隔,如:172.16.254.1
;[3] IPv6 由十六进制数字组成,以冒号分割,如:2001:db8:0:1234:0:567:8:1
。[4]
- What is CoreDataManager in iOS?
- 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?
类和结构体的不同点:
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
- explaining design patterns
设计模式 简单理解
- 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 响应链
- reverse words in a sentence
151. Reverse Words in a String
const reverseWords = s => s.trim().split(/\s+/).reverse().join(' ');
- Permutations of a word (全排列)
46. Permutations - leetcode
/**
* @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 :
- 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?