Skip to main content

Posts

(CI) Jenkins PMD report for Objective-C with OCLint

OCLint is a  static code analysis  tool for improving quality and reducing defects by inspecting C, C++ and Objective-C code and looking for potential problems like: Possible bugs - empty if/else/try/catch/finally statements Unused code - unused local variables and parameters Complicated code - high cyclomatic complexity, NPath complexity and high NCSS Redundant code - redundant if statement and useless parentheses Code smells - long method and long parameter list Bad practices - inverted logic and parameter reassignment ... Installation: Step 1 : Download "oclint-0.x.x-x86_64-darwin-15.0.0.tar" from the downloads page . Unzip this file and see as below: Step 2: Copying OCLint to System PATH. Open Terminal prompt, and input some commands lines as follows: $ cd /Downloads/oclint-0.10.x $ cp bin/oclint* /usr/local/bin/ $ cp -rp lib/* /usr/local/lib/ Other option: Directly adding to PATH at here . Checking installation:   Open a new terminal prompt, and execute  o...

Improve Unit Test with OCMock

Adding OCMock to your project: Step 1: Download a release from the  downloads page . Step 2: Setup OCMock from the  iOS page . Step 3: Add an import to your unit tests. #import <OCMock/OCMock.h> Contents: Example classes Expect-run-verify Verify-after-running Void-Method Return-Method Block Delegate and DataSource Notification (Observer) Exception Advanced topics 1. Example classes Let's assume we are writing a little application. Our application have two classes as ClassA and ClassB. ClassB inherit from ClassA. They have some methods as below: 2. Expect-run-verify This is the original approach to mocking. First the mock object is set up with expectations, then the code under test is run, and afterwards the expectations are verified. If an expected method has not been invoked, or has not been invoked with the right arguments, then an error is reported. As shown it is possible to use argument constraints in the expect statement. Strict mocks can be created for classes and...

[Tip] Simulate various network conditions with "Network Link Conditioner"

Xcode provides a tool called Network Link Conditioner that can simulate various network conditions , including reduced bandwidth, high latency, DNS delays, packet loss, and so on. Before you ship any software that uses networking, you should install this tool, enable it, then run your software to see how it performs under real-world conditions. Here are a few things to test: Make sure your software remains usable even with lousy bandwidth. Tune your bandwidth consumption as much as you can. Increase the latency to three or four seconds. Make sure that any user-initiated operation is delayed by only a few seconds, not by a few minutes. When the network connection drops packets, your software should continue to function, just more slowly. Enabling  Network Link Conditioner  on iOS device: - Step 1: Open Settings -> Select Developer -> Select Status in group name ( Network link conditioner ) : - Step 2: Enable Network Link Conditioner -> Choose a profile of network ...

[Structural Pattern] Adapter pattern trong Objective C

Khái niệm: chuyển đổi interface của một class thành interface khác mà bạn trông đợi, để có thể tương thích với những interface đã có sẵn trước đó. Adapter cho phép những class hoạt động chung với nhau bất kể những interface của những class không tương thích với nhau. Cấu trúc lớp: Ví dụ mình có sơ đồ lớp như sau: - Adapter theo quan hệ Navigability hoặc Aggregation hay Composition như nhau: - Adapter theo quan hệ kế thừa Generalization hay Realization như sau: Cách sử dụng: Chúng ta thường dùng Adapter trong việc bảo trì ứng dụng. Chúng ta không thể viết code vào những đối tượng có sẵn trước đó nhưng vẫn phải đảm bảo hệ thống chạy tương thích với những lớp đó hay có thể sửa mới thì chúng ta ứng dụng Adapter vào. Code ví dụ trong Objective C:

[UML Class relationships] Phân biệt quan hệ Generalization và Realization trong Objective C và Swift

2 quan hệ này tương đối dễ phân biệt và thực thi. Generalization :  giống quan hệ kế thừa trong những ngôn ngữ lập trình. Theo sơ đồ trên thì ClassB đang kế thừa ClassA Code ví dụ trong Objective C : Code ví dụ trong Swift: Realization :  là quan hệ thực thi 1 interface. Theo sơ đồ trên thì ClassA thực thi những chức năng được định nghĩa sẵn trong InterfaceA. Code ví dụ trong Objective C: Code ví dụ trong Swift:

[UML Class relationships] Phân biệt quan hệ Aggregation và Composition trong Objective C và Swift

Aggregation ClassA có dùng lớp và giữa ClassB nhưng không cho lớp khác truy xuất ClassB thông qua ClassA.  Trong Objective C ta dùng thuộc tính là weak để thể hiện mối quan hệ này, code ví dụ như sau: Ngoài ra ta còn có sơ đồ Aggregation - Navigability: Theo sơ đồ này thì ta dùng property để lớp khác có thể truy xuất ClassB thông qua ClassA. Code ví dụ trong Objective C như: Unit Test giả sử ta huỷ đối tượng ClassB thì ClassA sẽ không gọi hàm trong ClassB được, do đó trong sơ đồ này nên dùng if để kiểm tra đối tượng khi truyền vào để tránh bị crash ứng dụng: Code ví dụ trong Swift như sau: Composition Composition giống như Aggregation nhưng giữ đối tượng theo kiểu strong, thì sẽ không phụ thuộc vào đối tượng truyền vào. Giống code ở trên nhưng ta thay weak thành strong thì sẽ thể hiện mối quan hệ này , ví dụ như sau: Cũng viết UnitTest như trên nhưng kết quả khi huỷ object tham chiếu thì đối tượng đó vẫn được giữ lại để thực hiện phương thức: Code ví dụ trong Swift như sau: Tà...

[UML Class relationships] Phân biệt quan hệ Association, Dependency và Navigability trong Objective C

Association :  Miêu tả 1 kết nối đơn giản giữa 2 lớp với nhau. Tuỳ theo từng trường hợp có cách thể hiện sơ đồ khác nhau. Mối quan hệ bình thường: Theo sơ đồ này thì ClassA có sử dụng ClassB hoặc ngược lại, nhưng không giữ đối tượng đó . Code ví dụ trong objective C như sau: Dependency: Thể hiện mối quan hệ phụ thuộc yếu giữa lớp này với lớp kia qua mũi tên và số thành phần thể hiện. Vì thế lớp này không cần phải giữ thực thể của lớp kia. Có thể truyền thông qua tham số hoặc tự khởi tạo trong chính lớp sử dụng. Theo sơ đồ này thì  ClassA có sử dụng ClassB, nhưng không giữ đối tượng đó . Code ví dụ trong objective C như sau: Navigability: Thể hiện mối quan hệ phụ thuộc mạnh giữa lớp này với lớp kia qua mũi tên và số thành phần thể hiện. Vì thế lớp này phải giữ thực thể của lớp kia chỉ được get chứ không cho set (readonly) . Mối quan hệ Association - Navigability (0..1): Theo sơ đồ này thì ClassA có sử dụng ClassB, và giữ đối tượng đó để có thể truy xuất bằng property. Nhưng C...