Senior Mobile Developer - Vale

Contact me via @cwtututu.


  • Home

  • Categories

  • Archives

  • Tags

  • About

What Do You Want to Do During Your Best Decade

Posted on Feb 4 2015   |  

One friend ever asked me :”Are you planning to stay home during your best decade?”

I answer to him :”Why not? It is the best thing for me to stay with my family during my best decade.”

Spring is coming.

Mix Auto Layout With Manual Layout Code

Posted on Dec 22 2014   |  
  1. Set translates​Autoresizing​Mask​Into​Constraints = NO on the view since you will position and size it manually, anyway :)
  2. Override layoutSubviews (or viewDidLayoutSubviews if you want to do this in a view controller), call super (which performs the Auto Layout step) and then perform any manual layout we want to add

How to Understand the '!' in the End of IBOutlet Properties in Swift

Posted on Dec 19 2014   |  

Let us see the follow code:

1
@IBOutlet var mapView: MKMapView!

As we all know, the mapView is a variable from Interface Builder, but why there is a ! behind the property, let’s analyze it.

Firstly, Let’s ask what is the meaning of ! behind of variable?

It means the variable is an optional type, further, it is an implicityly unwrapped optional which we don’t need unwrap and use directly.

Then, why we need an optional type here?

Because of a rule. We know the initializer function in Swift must initialize all of properties which are not optional type and Swift doesn’t “know” that Interface Builder is supplying the views at run time.

CLOC - Count Lines of Code on Mac

Posted on Dec 6 2014   |  
  1. Download the cloc-1.62.pl, rename it to cloc.pl then copy it.
  2. Open ‘finder’ and browse to usr/local/bin on your Mac partition
    paste it there
  3. In your favorite editor make new text file and paste the following:

    1
    2
    3
    4
    5
    6
     #!/bin/bash
    if [ "$1" == "/" ]; then
    echo "wrong input, cloc can't work on."
    else
    perl /usr/local/bin/cloc.pl "$1"
    fi
  4. Save it as ‘cloc’ [without extension] on same folder [/usr/local/bin]

  5. Ppen ‘terminal’ from ‘applications’
  6. Browse to the folder by typing ‘cd /usr/local/bin’
  7. Type the following ‘chmod +x cloc’ in order to give permission for the file
  8. We made to execute using cloc is so easy now, you only need to open terminal and type ‘cloc /path/to/your/project/‘.

screenshot:
image

Refer to: http://me.syrex.me/2011/10/cloc-count-lines-of-code-on-mac.html

Mantle With Core Data

Posted on Nov 2 2014   |  

#Conform to the Protocol
First, on our model we conform to the MTLManagedObjectSerializing protocol. This has 2 required methods, but we’ll actually need to implement 3 methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma mark - MTLManagedObjectSerializing

+ (NSString *)managedObjectEntityName {
return @"Episode";
}

+ (NSDictionary *)managedObjectKeysByPropertyKey {
return @{ };
}

+ (NSValueTransformer *)thumbnailImageUrlEntityAttributeTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^NSString *(NSURL *url) {
return [url description];
} reverseBlock:^NSURL *(NSString *urlString) {
return [NSURL URLWithString:urlString];
}];
}

+ (NSSet *)propertyKeysForManagedObjectUniquing {
return [NSSet setWithObject:@"episodeNumber"];
}

This is all we need to enable the feature for our models, including updating the records if they exist already.

#Serializing to Core Data
Given the we’ve converted from a JSON document to our model, we also want to convert that model into an NSManagedObject for saving. We can do this for each record by using the MTLManagedObjectAdapter class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[self fetchEpisodesWithCompletion:^(NSArray *episodes) {
NSManagedObjectContext *moc = [self.persistenceController newChildManagedObjectContext];
[episodes enumerateObjectsUsingBlock:^(Episode *episode, NSUInteger idx, BOOL *stop) {
NSError *insertError;
NSManagedObject *mob = [MTLManagedObjectAdapter managedObjectFromModel:episode
insertingIntoContext:moc
error:&insertError];

if (mob) {
NSLog(@"Mob: %@", mob);
} else {
NSLog(@"ERROR: %@", insertError);
}
}];

NSError *saveError;
if ([moc save:&saveError]) {
[self.persistenceController saveContextAndWait:YES completion:^(NSError *error) {
NSLog(@"Save completed (%@)", error);
}];
} else {
NSLog(@"Error saving: %@", saveError);
}
}];

Refer to http://nsscreencast.com/episodes/120-mantle-with-core-data

Don't Hesitate

Posted on Nov 2 2014   |  

Don’t hesitate any more, insist to do what you are good at, and do them better and better.

How to Open the Xcode Project Directly When We Use SourceTree

Posted on Oct 17 2014   |  

More and more projects are controlled by Git, then I start to use SourceTree as the Git GUI. So, many times, I like to open SourceTree first, not some forder, not Xcode. Now, there is a question on my table. After I open the SourceTree, I still need to cd to my xcode project forder, then open the .xcodepro or .xcodeworkspace file. It is boring, right?

So, I expect there is a way to simplify the workflow.

Now, it is coming.

1, created a small automator workflow. There are two workflow files I have written, you can download them in here

2, add Custom Actions on SourceTree.

Script to run: automator

Parameters: -D VariableName=$REPO /Path/To/Workflow.workflow

3, try it!

So, this is all.

Refer to: http://blog.sourcetreeapp.com/2012/02/08/custom-actions-more-power-to-you/

How to Calculate Table Cell Height Using Auto-Layout

Posted on Oct 13 2014   |  

No time to write it clearly, I just post some code which helps me finish it.

  1. Use Xib, not storyboard.
  2. Add the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

static TBGrowRecordCell *sizingCell = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
});
TBGrowRecordEntity *record = self.recordArray[indexPath.row];
sizingCell.contentLabel.text = record.content;
[sizingCell layoutIfNeeded];

//Notice that: this is "sizingCell.contentView", not "sizingCell"
CGFloat height = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}

Notice that: this is “sizingCell.contentView”, not “sizingCell”.

  1. Choosing the checkbox constraint to margins.

  1. Set lines to 0.
  2. choosing the checkbox explicit.

Done! The effect is that the UIlabel named sizingCell.contentLabel will resize by text’s length, and the UITableViewCell will resize by the UILabel.

Refer to:

Dynamic Table View Cell Height and Auto Layout

Using auto-layout to calculate table cell height

http://stackoverflow.com/questions/25265173/how-can-a-get-the-auto-layout-size-of-the-uicollectionviewcells-in-ios-8-syste

One or Two Things I Want to Do Recently

Posted on Oct 12 2014   |  

Image

During a long time, I am thinking which programming language I want to learn on next step, Ruby or javascript, Android or Swift, and many times I can’t make a decision. Ruby looks like very cool, but it is far from the region I contribute to, Android is near with the region, but sometimes I think it is boring.

Now, I don’t want to waste my time any more. So I decide to do the following things next step:

  1. Learn Swift.
  2. Develop a music app using Swift language.
  3. Rebuild the music app using Java language.

And then, Focus on mobile developing.

Pod Command Uses Wrong Ruby Path

Posted on Oct 8 2014   |  

Some days ago, I updated my Macbook Pro’s OS to 10.10, then I changed the default Ruby version to other verson rather than the system default version. Today, when I used the pod install to install some third libraries, I found the followed error:

1
-bash: /usr/local/bin/pod: /usr/local/Cellar/ruby/2.1.2_3/bin/ruby: bad interpreter: No such file or directory

Then I knew that the pod command used wrong ruby version.

Try many ways, but I still can’t solve it. Then I opened the cocoaPods Guides and found the followed text:

CocoaPods is built with Ruby and it will be installable with the default Ruby available on OS X. You can use a Ruby Version manager, however we recommend that you use the standard Ruby available on OS X unless you know what you're doing.

Ok, I am sorry for seeing the text too late. But now, how should I do? I have no idea. Maybe I can see what it is in the pod file.

Then I run which pod on the terminal.

1
2
shunzhu:~ vale$ which pod
/usr/local/bin/pod

Then run mate /usr/local/bin/pod:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/local/Cellar/ruby/2.1.2_3/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'cocoapods' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end

gem 'cocoapods', version
load Gem.bin_path('cocoapods', 'pod', version)

Maybe I find something, the code in the first line, it is very similar with the error hint:

1
-bash: /usr/local/bin/pod: /usr/local/Cellar/ruby/2.1.2_3/bin/ruby: bad interpreter: No such file or directory

Ok, let me change the first line code to this #!/usr/bin/ruby

Try pod again:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
shunzhu:~ vale$ pod
Setting up CocoaPods master repo
Setup completed
Usage:

$ pod COMMAND

CocoaPods, the Objective-C library package manager.

Commands:

+ init Generate a Podfile for the current directory.
+ install Install project dependencies to Podfile.lock versions
+ ipc Inter-process communication
+ lib Develop pods
+ list List pods
+ outdated Show outdated project dependencies
+ plugins Show available CocoaPods plugins
+ push Temporary alias for the `pod repo push` command
+ repo Manage spec-repositories
+ search Searches for pods
+ setup Setup the CocoaPods environment
+ spec Manage pod specs
+ trunk Interact with the CocoaPods API (e.g. publishing new
specs)
+ try Try a Pod!
+ update Update outdated project dependencies and create new
Podfile.lock

Options:

--silent Show nothing
--completion-script Print the auto-completion script
--version Show the version of the tool
--verbose Show more debugging information
--no-ansi Show output without ANSI codes
--help Show help banner of specified command
shunzhu:~ vale$

Until here, I think I have solved the issue. Thanks God!!!

1…456…8
Changwei

Changwei

I develop iOS/Android apps with Swift/Kotlin language.

80 posts
33 categories
34 tags
GitHub Twitter Weibo Linkedin Upwork peopleperhour
Creative Commons
© 2011 - 2020 Changwei
Powered by Hexo
Theme - NexT.Muse