CGPoint and CGRect are structures (versus objects) and therefore the old NSLog standby %@ will not work as expected.

Here is how each structure is defined:

struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
 
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;

If you attempt NSLog to use the traditional ‘print object’ notation such as this:

// Print point structure using NSLog
CGPoint cgPoint = CGPointMake(1, 11);
NSLog(@"%@", cgPoint);

the compiler will generate a warning: Format specifies type ‘id’ but the argument has type ‘CGPoint’ (aka ‘struct CGPoint’) 

Good news is, this is easy to fix:

NSLog(@"Point: %@", NSStringFromCGPoint(cgPoint));

The output of converting a CGPoint to an NSString looks as follows:

Point: {1, 11}

Output CGRect using NSLog

Likewise, CGRect is a structure and will also give NSLog troubles. The solution is similar:

// Print rect structure using NSLog
CGRect rect = CGRectMake(5, 5, 10, 10);
NSLog(@"Rect: %@", NSStringFromCGRect(rect));
Rect: {{5, 5}, {10, 10}}

CGRect, CFDictionaryRef and NSLog

When working with CGRect structures, there is another solution, you can convert the rect to a CFDictionaryRef and print as shown below:

NSLog(@"CFDictionaryRef: %@", CGRectCreateDictionaryRepresentation(rect));
CFDictionaryRef: {
Height = 10;
Width = 10;
X = 5;
Y = 5;
}

If for some reason you need to keep the dictionary object around, here is how you can so if you are using ARC:

1
2
3
4
5
# Create reference to immutable CFDictionary
CFDictionaryRef currentListingRef = CGRectCreateDictionaryRepresentation(rect);
 
# Create Dictionary object managed by ARC
NSDictionary *dict = CFBridgingRelease(currentListingRef);

The code on line 5 moves the CGRef to and Objective-C object, and also hands the memory management over to ARC.

How to Use NSLog to Debug CGRect and CGPoint的更多相关文章

  1. 关于OC中直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型

    关于OC直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型,我们完全可以把其转换为OC对象来进项打印调试,而不必对结构体中的成员变量进行打印.就好比我们可以使 ...

  2. iOS 保存CGRect,CGPoint到NSArray'的方法

    由于CGRect和CGPoint等对象是Struct,即结构体,不是继承于NSObject的,所以需要先用NSValue的方法,把他们转化成NSValue对象,之后就可以存入NSArray了! @in ...

  3. ios开发之--CGRect/CGSize/CGPoint/CGVector/CGAffineTransform/UIEdgeInsets/UIOffset和NSString之间的转换

    仅做记录,一个函数和字符串之间的互相转换 方法如下: UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UIKIT_EXTERN N ...

  4. ios CGRect

    /*     rect(x,y,width,height);     width, height正负代表了从原点的绘制方向,矩形的长宽都是取得绝对值     */            // Do a ...

  5. ios NSLog常见使用

    NSLog常见输出格式 Table 1  Format specifiers supported by the NSString formatting methods and CFString for ...

  6. CGPoint、CGSize、CGRect and UIView

    首先要弄懂几个基本的概念. 一)三个结构体:CGPoint.CGSize.CGRect 1. CGPoint /* Points. */ struct CGPoint { CGFloat x; CGF ...

  7. iOS开发之 在release版本禁止输出NSLog内容

    因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...

  8. UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏

    UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...

  9. Xcode/iOS: 如何判断代码运行在DEBUG还是RELEASE模式下?

    原帖链接:http://stackoverflow.com/a/9063469 首先确定下项目的 Build Settings 是否已经设置过宏定义 DEBUG,如何看呢? 点击 Build Sett ...

随机推荐

  1. Flash-使用变形面板制作花朵

    在Flash中利用"变形"面板的"重置选取和变形"button(在变形面板右下角),能够自己主动将对象进行创造性变形地画图 步骤: (1)先导入一幅图像 (2) ...

  2. 使用psftp.exe

    使用psftp.exe 点击打开psftp.exe,出现如下图的命令窗口.  

  3. Windows Phone开发(20):当MediaElement和VideoBrush合作的时候

    原文:Windows Phone开发(20):当MediaElement和VideoBrush合作的时候 前面说的那么多控件都是"静态"的,都是"哑吧"的,今天 ...

  4. 【剑指offer】设置在最小数目的阵列

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/28128551 题目描写叙述: 输入一个正整数数组,把数组里全部数字拼接起来排成一个数.打印 ...

  5. Ubuntu下安装vmware 9.0 + 注册码

    先附上一些注册码到时使用: NA0UF-DUH00-QZHM0-MU17K-CC824 4F469-F024Q-CZ8R9-DL1N0-13C6W HF261-0HL40-FZX21-F9AQ2-0C ...

  6. 【oracle案件】ORA-19502,ORA-27072

    1.1.1. ORA-19502,ORA-27072 日期:2014-05-12 00:12 环境:试验机 [错误号] $ oerr ora 19502 19502, 00000, "wri ...

  7. 排列组合相关算法 python

    获取指定长度得全部序列 通过事件来表述这个序列,即n重伯努利实验(二项分布)的全部可能结果.比如时间a表示为: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 假设每次实验为从 ...

  8. Java Web整合开发(80) -- EJB & WebService

    1. jdk-6u18-windows-i586-p.execlasspath: .;%JAVA_HOME%lib/tools.jar;%JAVA_HOME%lib/dt.jar;%JAVA_HOME ...

  9. FTP文件操作之获取文件列表

    前面已经介绍了很多关于FTP对文件的操作,今天再跟大家介绍一个获取文件列表的功能.这个功能应该算是最简单的一个了,它只是获取了一下文件信息,而没有进行实质上的数据传输. 下面是是该功能的核心代码:   ...

  10. leetcode——Evaluate Reverse Polish Notation 求算式值(AC)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...