Hi. I’m Scott Zhu

iOS & Android Developer. Geek. Dreamer.

How to use NSCoding

Let’s say you want to archive or save a object called City. The City.h is declared as below:

1
2
3
4
5
6
7
8
@interface City : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *state;
@property (nonatomic) NSInteger population;
@property (nonatomic, strong) NSString *note;

@end

The first thing you need to do is to make your class confirm to NSCoding protocol. This is simply telling the system,

“Ok, i’m gonna use NSCoding to help archive and unarchive this class.”

Next, you need to add two methods to your City.h, which will actually help you doing the archive and unarchive works.

1
2
3
- (id)initWithCoder:(NSCoder *)aDecoder;

- (void)encodeWithCoder:(NSCoder *)aCoder;

You will need to implement these two methods by yourself, but you will never call these two method manually. We will explain it later. Add implementation of these two methods to City.m,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.state forKey:@"state"];
    [aCoder encodeObject:[NSNumber numberWithInteger:self.population] forKey:@"population"];
    [aCoder encodeObject:self.note forKey:@"note"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self)
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.state = [aDecoder decodeObjectForKey:@"state"];
        self.population = [[aDecoder decodeObjectForKey:@"population"] integerValue];
        self.note = [aDecoder decodeObjectForKey:@"note"];
    }
    return self;
}

Length of Last Word

Description

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example, Given s = "Hello World",

return 5.


Tips:

  • Need confirm does one character count a word, in this case YES
  • Start from the end of the string, count++ until you meet a ' '
  • "word " and "word "
  • " "

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
    int lengthOfLastWord(const char *s)
    {
        if(!s) return 0;
        int length = strlen(s);
        int count = 0;
        for(int i = length - 1; i >= 0; i-- )
        {
            if(s[i] == ' ' && count >= 1) return count;
            if(s[i] != ' ') count++;
        }
        return count;
    }
};

How do Chinese people get a boy/girl friend

Several months ago, i ended up the relationship with my ex-girlfriend(well not me, it’s she actually). It’s been tough for me in past 4 months. But this is really not what i want to talk about here. A more interesting topic comes up is how to find a new girlfriend then.

When looking for a girlfriend, people tends to look around first. It’s like when we are in the school we look around our classmates, when we already in the career we look around our colleagues. Basically, above all, we look for girlfriend among people we already know or people we already familiar with. University is the best place to get a girlfriend, no one would deny that. Lots students hanging around with free thinkings, lots different activities gathered students together with same interest. We can meet as many girls as you want in the university. But i beg you better got a girlfriend in the university, because otherwise after you graduate, things became extremely hard. Still we look for people around us, but this time it comes to the colleagues. It really depends on the company we work for. Normally, the amount of employees is much smaller than the amount of the students in the university, and usually in a same company where all people are doing the same kind of work, the employees are mostly in the same sex. The number of heterosexual colleagues is quite limited. What’s more, even within those heterosexual colleagues, most them already got a boyfriend when they were in the university or they already got married….what the fuck??

While people failed to start a relationship with people they know or people they familiar with, it comes to the second solution, which is join different social activities. Normally a traditional Chinese guy doesn’t join many social activities, they prefer to stay at home much than go out. The funny thing is that those only activities they participated in were with people they already know. Like meet and join a dinner with high school classmates all together, or meet with collage classmates in a dinner as well, or meet with colleagues in a KTV(a place where you and your friend can sing together). Most Chinese people barely join any activities with strangers to meet new friends. Unlike western countries, people tend to go to a bar at weekend to meet different people. In China, people are still too shy to perform like that. Yes it’s true that there is also a group of people in China nowadays who got western thinking, they went to the bar, make friends with different people sharing different ideas which is good. And that’s why foreigners in China now are saying that Chinese people nowadays are more and more open, because they can met more and more Chinese people in a bar, and they talk like really western. But still the amount of those people is really small compare to the big population in the city.

Boys prefer to stay at home other than go out like i said. But they are also human, they can not escape the human nature – they like girls. So the best solution would be just stay at home and get me a girlfriend! It’s not funny, it’s true. Some really smart Chinese companies dug this need out, like MOMO and WeChat. With these mobile app, they can easily find different people around them, including girls :) Lots people spent day and night on those little apps, they start chat with strange girls(with some pictures anyway) by sending a simple greeting like ‘Hi’. If the girl is interested in the boy, she will write back, then they start to chat, step by step. They probably will go out with each other depends on if the girl is interested in the boy enough. But the possibility is surely low, because girls are much more shy than boys. It’s difficult to ask them out.

Finally, what if all those above don’t work? It’s pretty common, all of those couldn’t work, old boys couldn’t find a girlfriend. It comes to blind dating, i’m not sure what this word means in western countries, but in china it means your parents or your close friend arrange a dating for you with a girl who haven’t get a boyfriend yet also. Actually, parents are much more worried about you getting a girlfriend than your self in China. Because it matters when they gonna get a grandchild, it also matters their reputation among their friend. Because it’s too ashamed for they if their child could’t find a girlfriend or boyfriend in such age like around 27,28.