Xcode中将环境变量当作调试开关

在开发新功能的过程中有时会为了进行一些快速的测试,会在上下文中修改变量的值或者执行一些方法来配置测试环境, 一但这么做就有可能忘记移除测试代码,将其编译到最终的发由的产品中,于是为了降低风险用#if DEBUG来包装了一下测试代码, 把风险控制在开发环境。
但是这样依然有可能干扰团队其它成员的开发。如果用宏定义开关来控制,偶尔也会因把配置文件提交而造成干扰。
那么如何做才能减少这些弊端,既能配置测试环境又不会不小心把测试代码提交到版本库呢?
想到Xcode的工程文件中的非公享Scheme是分用户的,那么里面的配置是不会扩散到团队的,可以从这里入手,看了下Xcode的Scheme配置,可以指定环境变量,在应用中就可以通过环境变量来配置测试环境,一来可以不干扰其它成员,二来也可以把一些常用的测试代码共享给别的人用。使用POSIX的getenv方法也比较方便进行标记判断,也可以带一些参数上去。
比如:

屏幕快照 2016-04-07 22.41.39


#if DEBUG
if (getenv("reset")) {
[Debugger resetApp];
}
#endif


#if DEBUG
char *tab_index = getenv("INITIAL_TAB");
if (tab_index) {
[_tabBarController setSelectedIndex:atoi(tab_index)];
}
#endif

这样就可以通过在scheme中勾选要调试的选项来进行测试了。勾选了reset,那么每次启动时就清掉所有的数据,勾选了INITIAL_TAB就会在打开App的时候自动进入对应的Tab。

除此之外如果简单的修改一些变量,可以在Xcode的断点命令中执行expr var = val这种操作,也是不会干扰到正常代码的一个方法。

Xcode 5.1 下 提示 duplicate declaration of method 的解决方法

升级到Xcode5.1后遇到一个奇怪的问题: llvm5.1在编译SVProgressHUD时会报错,提示类方法与实例方法重复,即 “duplicate declaration of method” 对比了两个方法,发现两个方法中一个参数声明为CGFloat一个声明为float, 两个类型不匹配。估计被认为是尝试重载方法,修改一致后这个error就消失了。经尝试只有方法在类的Extension中声明时会有这个问题,如果在Category或者@interfacer中声明则不会有此问题。

objc block是如何实现的

Objective-C中引入的Block给代码的编写以及多线程的处理带来了很多方便,但它是如何实现的呢?可以借助clang的–rewrite-objc来揭开她的面纱。

因为下面的代码会涉及一点c++ 在揭开之前先说一点c++的简单知识。
struct在c++中与class一样都是声明一个类,但struct中默认都声明为public。既然是个类struct就可以有自己的构造函数。

struct Foo{
  int bar;
  Foo(int _bar) {
    bar = _bar
  };
}
Foo f = Foo(1);
int x = f.bar;

这里Foo(int _bar)就是Foo的构造函数,与objc中的init类似。

关于struct的说明就这些,以下把struct都叫类, 下面来看block

看一段代码,给hi赋值了一个打印”hello!”的block,然后调用它

#import 
int main()
{
	void (^hi)(int a) = ^(int a){printf("hello %d\n", a);};
	hi(1);
	return 0;
}

使用clang -rewrite-objc test.m得到test.cpp, 看下转化后的代码, 这里省去部分无关的代码。

# 2 "test.m" 2

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

#line 4 "test.m"
static void __main_block_func_0(struct __main_block_impl_0 *__cself, int a) {
printf("hello %d\n", a);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};

#line 2 "test.m"
int main()
{
 void (*hi)(int a) = (void (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA);
 ((void (*)(__block_impl *, int))((__block_impl *)hi)->FuncPtr)((__block_impl *)hi, 1);
 return 0;
}

我们看到之前的代码变成了下面的样子

 void (*hi)(int a) = (void (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA);
 ((void (*)(__block_impl *, int))((__block_impl *)hi)->FuncPtr)((__block_impl *)hi, 1);

看到hi()的调用实际是调用了hi->FuncPtr所指向的函数并把hi和原来的能数作为参数列表。
那么FuncPtr指向了哪个函数呢,从__main_block_impl_0的构造函数和hi的创建可以看到FuncPtr指到了__main_block_func_0。下面看一下__main_block_func_0的实现

static void __main_block_func_0(struct __main_block_impl_0 *__cself, int a) {
printf("hello %d\n", a);
}

这里就是之前写的block里面的内容,那么这里为什么会把block自己传递进来了,这个后面再看。

到此为至,看到了之前写的block的内容会变成一个c函数,原来引用的block的变量会指向到一个__main_block_impl_0的实例上。__main_block_impl_0会有一个指针指向到变成的c函数上。

block有个特点是可以抓住block外的变量,比如下面这段代码:

#import 
int main()
{
	int foo = 1;
	void (^hi)() = ^{printf("hello! %d\n", foo);};
	hi();
	return 0;
}

rewrite后

# 2 "test1.m" 2

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int foo;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _foo, int flags=0) : foo(_foo) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

#line 5 "test1.m"
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  int foo = __cself->foo; // bound by copy
printf("hello! %d\n", foo);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};

#line 2 "test1.m"
int main()
{
 int foo = 1;
 void (*hi)() = (void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, foo);
 ((void (*)(__block_impl *))((__block_impl *)hi)->FuncPtr)((__block_impl *)hi);
 return 0;
}

对比之前的代码,发现block实例化的时候把foo这个变量传递了进去并保存在block的实例中,在block对应的函数中,我们看到了如何使用这个变量

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  int foo = __cself->foo; // bound by copy
  printf("hello! %d\n", foo);
}

所以block的外部变量引用是在初始化的时候会把变量拷贝一份到block实例中,在block调用时,再使用这一份拷贝的数据。

block还有一个功能修改一个外部变量的值,即用__block修饰的变量,再来看一段示例代码

#import 
#import 
int main()
{
   	__block int foo = 1;
	void (^hi)() = ^{foo=2;};
	hi();
	printf("foo: %d\n", foo);
	return 0;
}

clang test2.m -lobjc -rewrite-objc

# 2 "test2.m" 2
struct __Block_byref_foo_0 {
  void *__isa;
__Block_byref_foo_0 *__forwarding;
 int __flags;
 int __size;
 int foo;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  __Block_byref_foo_0 *foo; // by ref
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_foo_0 *_foo, int flags=0) : foo(_foo->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

#line 5 "test2.m"
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  __Block_byref_foo_0 *foo = __cself->foo; // bound by ref
(foo->__forwarding->foo)=2;}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->foo, (void*)src->foo, 8/*BLOCK_FIELD_IS_BYREF*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->foo, 8/*BLOCK_FIELD_IS_BYREF*/);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
  void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

#line 2 "test2.m"
int main()
{
    __attribute__((__blocks__(byref))) __Block_byref_foo_0 foo = {(void*)0,(__Block_byref_foo_0 *)&foo, 0, sizeof(__Block_byref_foo_0), 1};
 void (*hi)() = (void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_foo_0 *)&foo, 570425344);
 ((void (*)(__block_impl *))((__block_impl *)hi)->FuncPtr)((__block_impl *)hi);
 printf("foo: %d\n", (foo.__forwarding->foo));
 return 0;
}

这次变化略大,还是从main开始看。
原来的foo从int变成了__Block_byref_foo_0, 其它的调用与之前差不多,注意到后面对foo的操作都是在修改或获取foo.__forwarding->foo。

看一下__Block_byref_foo_0的定义,

struct __Block_byref_foo_0 {
  void *__isa;
 __Block_byref_foo_0 *__forwarding;
 int __flags;
 int __size;
 int foo;
};

和实例化所使用的方法

__Block_byref_foo_0 foo = {(void*)0,(__Block_byref_foo_0 *)&foo, 0, sizeof(__Block_byref_foo_0), 1}

注意到这里的__forwading引用了foo也就是其自身, 为什么要这样做呢,为了在block copy到堆后可以修改block外的变量。

从上面的代码可以看到block的创建是在栈上,如果一旦函数结束foo和block就会被释放掉,所以需要在作用域外使用block时会对block做一次copy操作,把block从栈拷贝到堆上,拷贝完成后foo.__forwarding会指向堆里的foo这样,堆中的foo.__forwarding指向自身,后面需对foo的操作都会对堆中的内存进行修改,即使作用域结束栈中的变量无效,也能正常对__block修饰的变量进行读写了。

简单的介绍了一下block的原理与automatic variable capturing的原理. 再看下下面几个会出现异常的代码。

typedef void (^blk_t)(void);
blk_t createBlock(void) {
  int a = 1;
  return ^{ printf("a: %d\n", a);}
}

int main(int argc, char **argv) {
  createBlock()();
  return 0;
}

这段代码在非arc下运行时会出问题。因为a在createBlock后被干掉了,如果声明为__block int a是否会出问题呢?感兴趣的盆友可以自行验证一下。

Xcode的环境变量

http://stackoverflow.com/questions/6910901/canonical-list-of-xcode-environment-variables

Variable Example
PATH “/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin”
LANG en_US.US-ASCII
IPHONEOS_DEPLOYMENT_TARGET 4.1
ACTION build
AD_HOC_CODE_SIGNING_ALLOWED NO
ALTERNATE_GROUP staff
ALTERNATE_MODE u+w,go-w,a+rX
ALTERNATE_OWNER username
ALWAYS_SEARCH_USER_PATHS YES
APPLE_INTERNAL_DEVELOPER_DIR /AppleInternal/Developer
APPLE_INTERNAL_DIR /AppleInternal
APPLE_INTERNAL_DOCUMENTATION_DIR /AppleInternal/Documentation
APPLE_INTERNAL_LIBRARY_DIR /AppleInternal/Library
APPLE_INTERNAL_TOOLS /AppleInternal/Developer/Tools
APPLY_RULES_IN_COPY_FILES NO
ARCHS “armv6
ARCHS_STANDARD_32_64_BIT “armv6
ARCHS_STANDARD_32_BIT “armv6
ARCHS_UNIVERSAL_IPHONE_OS armv7
AVAILABLE_PLATFORMS “iphonesimulator
BUILD_COMPONENTS “headers
BUILD_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
BUILD_ROOT “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
BUILD_STYLE
BUILD_VARIANTS normal
BUILT_PRODUCTS_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
CACHE_ROOT /var/folders/2x/rvb2r9s16mq6r318zxvn0lk80000gn/C/com.apple.Xcode.501
CCHROOT /var/folders/2x/rvb2r9s16mq6r318zxvn0lk80000gn/C/com.apple.Xcode.501
CHMOD /bin/chmod
CHOWN /usr/sbin/chown
CLASS_FILE_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
CLEAN_PRECOMPS YES
CLONE_HEADERS NO
CODESIGNING_FOLDER_PATH “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
CODE_SIGNING_ALLOWED YES
CODE_SIGNING_REQUIRED YES
CODE_SIGN_CONTEXT_CLASS XCiPhoneOSCodeSignContext
CODE_SIGN_IDENTITY “iPhone
COMBINE_HIDPI_IMAGES NO
COMPOSITE_SDK_DIRS /var/folders/2x/rvb2r9s16mq6r318zxvn0lk80000gn/C/com.apple.Xcode.501/CompositeSDKs
COMPRESS_PNG_FILES YES
CONFIGURATION Distribution
CONFIGURATION_BUILD_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
CONFIGURATION_TEMP_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
CONTENTS_FOLDER_PATH project.app/Contents
COPYING_PRESERVES_HFS_DATA NO
COPY_PHASE_STRIP YES
COPY_RESOURCES_FROM_STATIC_FRAMEWORKS YES
CP /bin/cp
CURRENT_ARCH armv7
CURRENT_VARIANT normal
DEAD_CODE_STRIPPING YES
DEBUGGING_SYMBOLS YES
DEBUG_INFORMATION_FORMAT dwarf-with-dsym
DEPLOYMENT_LOCATION YES
DEPLOYMENT_POSTPROCESSING YES
DERIVED_FILES_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
DERIVED_FILE_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
DERIVED_SOURCES_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
DEVELOPER_APPLICATIONS_DIR /Developer/Applications
DEVELOPER_BIN_DIR /Developer/usr/bin
DEVELOPER_DIR /Developer
DEVELOPER_FRAMEWORKS_DIR /Developer/Library/Frameworks
DEVELOPER_FRAMEWORKS_DIR_QUOTED “\”/Developer/Library/Frameworks\””
DEVELOPER_LIBRARY_DIR /Developer/Library
DEVELOPER_SDK_DIR /Developer/SDKs
DEVELOPER_TOOLS_DIR /Developer/Tools
DEVELOPER_USR_DIR /Developer/usr
DEVELOPMENT_LANGUAGE English
DOCUMENTATION_FOLDER_PATH project.app/English.lproj/Documentation
DO_HEADER_SCANNING_IN_JAM NO
DSTROOT “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
DWARF_DSYM_FILE_NAME project.app.dSYM
DWARF_DSYM_FILE_SHOULD_ACCOMPANY_PRODUCT NO
DWARF_DSYM_FOLDER_PATH “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
EFFECTIVE_PLATFORM_NAME -iphoneos
EMBEDDED_PROFILE_NAME embedded.mobileprovision
ENABLE_HEADER_DEPENDENCIES YES
ENABLE_OPENMP_SUPPORT NO
ENTITLEMENTS_ALLOWED YES
ENTITLEMENTS_REQUIRED YES
EXCLUDED_INSTALLSRC_SUBDIRECTORY_PATTERNS “.svn
EXECUTABLES_FOLDER_PATH project.app/Executables
EXECUTABLE_FOLDER_PATH project.app
EXECUTABLE_NAME project
EXECUTABLE_PATH project.app/project
FILE_LIST “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
FIXED_FILES_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
FRAMEWORKS_FOLDER_PATH project.app/Frameworks
FRAMEWORK_FLAG_PREFIX -framework
FRAMEWORK_SEARCH_PATHS “\”/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
FRAMEWORK_VERSION A
FULL_PRODUCT_NAME project.app
GCC3_VERSION 3.3
GCC_C_LANGUAGE_STANDARD gnu99
GCC_INLINES_ARE_PRIVATE_EXTERN YES
GCC_PFE_FILE_C_DIALECTS “c
GCC_PRECOMPILE_PREFIX_HEADER YES
GCC_PREFIX_HEADER project/Prefix.pch
GCC_PREPROCESSOR_DEFINITIONS “NDEBUG
GCC_SYMBOLS_PRIVATE_EXTERN YES
GCC_THUMB_SUPPORT YES
GCC_TREAT_WARNINGS_AS_ERRORS NO
GCC_VERSION com.apple.compilers.llvm.clang.1_0
GCC_VERSION_IDENTIFIER com_apple_compilers_llvm_clang_1_0
GCC_WARN_ABOUT_RETURN_TYPE YES
GCC_WARN_UNUSED_FUNCTION YES
GCC_WARN_UNUSED_VARIABLE YES
GENERATE_MASTER_OBJECT_FILE NO
GENERATE_PKGINFO_FILE YES
GENERATE_PROFILING_CODE NO
GID 20
GROUP staff
INPUT_FILE_BASE Default
INPUT_FILE_DIR “/Volumes/Development/Project
INPUT_FILE_NAME Default.png
INPUT_FILE_PATH “/Volumes/Development/Project
SCRIPT_INPUT_FILE “/Volumes/Development/Project
SCRIPT_OUTPUT_FILE_0 “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
EXCLUDED_RECURSIVE_SEARCH_PATH_SUBDIRECTORIES “*.nib
HEADERMAP_INCLUDES_FLAT_ENTRIES_FOR_TARGET_BEING_BUILT YES
HEADERMAP_INCLUDES_FRAMEWORK_ENTRIES_FOR_ALL_PRODUCT_TYPES YES
HEADERMAP_INCLUDES_NONPUBLIC_NONPRIVATE_HEADERS YES
HEADERMAP_INCLUDES_PROJECT_HEADERS YES
HEADER_SEARCH_PATHS “\”/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
ICONV /usr/bin/iconv
INFOPLIST_EXPAND_BUILD_SETTINGS YES
INFOPLIST_FILE project/Resources/Info.plist
INFOPLIST_OUTPUT_FORMAT binary
INFOPLIST_PATH project.app/Info.plist
INFOPLIST_PREPROCESS NO
INFOSTRINGS_PATH project.app/English.lproj/InfoPlist.strings
INPUT_FILE_REGION_PATH_COMPONENT
INPUT_FILE_SUFFIX .png
INSTALL_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
INSTALL_GROUP staff
INSTALL_MODE_FLAG u+w,go-w,a+rX
INSTALL_OWNER username
INSTALL_PATH /Applications
INSTALL_ROOT “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
JAVAC_DEFAULT_FLAGS “-J-Xms64m
JAVA_APP_STUB /System/Library/Frameworks/JavaVM.framework/Resources/MacOS/JavaApplicationStub
JAVA_ARCHIVE_CLASSES YES
JAVA_ARCHIVE_TYPE JAR
JAVA_COMPILER /usr/bin/javac
JAVA_FOLDER_PATH project.app/Java
JAVA_FRAMEWORK_RESOURCES_DIRS Resources
JAVA_JAR_FLAGS cv
JAVA_SOURCE_SUBDIR .
JAVA_USE_DEPENDENCIES YES
JAVA_ZIP_FLAGS -urg
JIKES_DEFAULT_FLAGS “+E
KEEP_PRIVATE_EXTERNS NO
LD_GENERATE_MAP_FILE NO
LD_MAP_FILE_PATH “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
LD_NO_PIE NO
LD_OPENMP_FLAGS -fopenmp
LEGACY_DEVELOPER_DIR /Developer/Library/Xcode/PrivatePlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer
LEX /Developer/usr/bin/lex
LIBRARY_FLAG_NOSPACE YES
LIBRARY_FLAG_PREFIX -l
LIBRARY_SEARCH_PATHS “\”/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
LINKER_DISPLAYS_MANGLED_NAMES NO
LINK_FILE_LIST_normal_armv6 “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
LINK_FILE_LIST_normal_armv7 “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
LINK_WITH_STANDARD_LIBRARIES YES
LOCALIZED_RESOURCES_FOLDER_PATH project.app/English.lproj
LOCAL_ADMIN_APPS_DIR /Applications/Utilities
LOCAL_APPS_DIR /Applications
LOCAL_DEVELOPER_DIR /Library/Developer
LOCAL_LIBRARY_DIR /Library
MACH_O_TYPE mh_execute
MAC_OS_X_PRODUCT_BUILD_VERSION 11A511
MAC_OS_X_VERSION_ACTUAL 1070
MAC_OS_X_VERSION_MAJOR 1070
MAC_OS_X_VERSION_MINOR 0700
NATIVE_ARCH armv6
NATIVE_ARCH_32_BIT i386
NATIVE_ARCH_64_BIT x86_64
NATIVE_ARCH_ACTUAL x86_64
NO_COMMON YES
OBJECT_FILE_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
OBJECT_FILE_DIR_normal “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
OBJROOT “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
ONLY_ACTIVE_ARCH NO
OPTIMIZATION_LEVEL 0
OS MACOS
OSAC /usr/bin/osacompile
OTHER_CFLAGS -DNS_BLOCK_ASSERTIONS=1
OTHER_CPLUSPLUSFLAGS -DNS_BLOCK_ASSERTIONS=1
OTHER_INPUT_FILE_FLAGS
OTHER_LDFLAGS -lz
PACKAGE_TYPE com.apple.package-type.wrapper.application
PASCAL_STRINGS YES
PATH_PREFIXES_EXCLUDED_FROM_HEADER_DEPENDENCIES “/usr/include
PBDEVELOPMENTPLIST_PATH project.app/pbdevelopment.plist
PFE_FILE_C_DIALECTS “c
PKGINFO_FILE_PATH “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
PKGINFO_PATH project.app/PkgInfo
PLATFORM_DEVELOPER_APPLICATIONS_DIR /Developer/Platforms/iPhoneOS.platform/Developer/Applications
PLATFORM_DEVELOPER_BIN_DIR /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin
PLATFORM_DEVELOPER_LIBRARY_DIR /Developer/Library/Xcode/PrivatePlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library
PLATFORM_DEVELOPER_SDK_DIR /Developer/Platforms/iPhoneOS.platform/Developer/SDKs
PLATFORM_DEVELOPER_TOOLS_DIR /Developer/Platforms/iPhoneOS.platform/Developer/Tools
PLATFORM_DEVELOPER_USR_DIR /Developer/Platforms/iPhoneOS.platform/Developer/usr
PLATFORM_DIR /Developer/Platforms/iPhoneOS.platform
PLATFORM_NAME iphoneos
PLATFORM_PREFERRED_ARCH i386
PLATFORM_PRODUCT_BUILD_VERSION 8H7
PLIST_FILE_OUTPUT_FORMAT binary
PLUGINS_FOLDER_PATH project.app/PlugIns
PRECOMPS_INCLUDE_HEADERS_FROM_BUILT_PRODUCTS_DIR YES
PRECOMP_DESTINATION_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
PRESERVE_DEAD_CODE_INITS_AND_TERMS NO
PRIVATE_HEADERS_FOLDER_PATH project.app/PrivateHeaders
PRODUCT_NAME project
PRODUCT_SETTINGS_PATH “/Volumes/Development/Project
PRODUCT_TYPE com.apple.product-type.application
PROFILING_CODE NO
PROJECT project
PROJECT_DERIVED_FILE_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
PROJECT_DIR “/Volumes/Development/Project
PROJECT_FILE_PATH “/Volumes/Development/Project
PROJECT_NAME project
PROJECT_TEMP_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
PROVISIONING_PROFILE_REQUIRED YES
PUBLIC_HEADERS_FOLDER_PATH project.app/Headers
RECURSIVE_SEARCH_PATHS_FOLLOW_SYMLINKS YES
REMOVE_CVS_FROM_RESOURCES YES
REMOVE_GIT_FROM_RESOURCES YES
REMOVE_SVN_FROM_RESOURCES YES
RESOURCE_RULES_REQUIRED YES
REZ_COLLECTOR_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
REZ_OBJECTS_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
REZ_SEARCH_PATHS “\”/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
RUN_CLANG_STATIC_ANALYZER NO
SCAN_ALL_SOURCE_FILES_FOR_INCLUDES NO
SCRIPTS_FOLDER_PATH project.app/Scripts
SCRIPT_INPUT_FILE “/Volumes/Development/Project
SCRIPT_OUTPUT_FILE_0 “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
SCRIPT_OUTPUT_FILE_COUNT 1
SDKROOT /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
SDK_DIR /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
SDK_NAME iphoneos4.3
SDK_PRODUCT_BUILD_VERSION 8H7
SED /usr/bin/sed
SEPARATE_STRIP NO
SEPARATE_SYMBOL_EDIT NO
SET_DIR_MODE_OWNER_GROUP YES
SET_FILE_MODE_OWNER_GROUP NO
SHALLOW_BUNDLE YES
SHARED_DERIVED_FILE_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
SHARED_FRAMEWORKS_FOLDER_PATH project.app/SharedFrameworks
SHARED_PRECOMPS_DIR /Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/Build/PrecompiledHeaders
SHARED_SUPPORT_FOLDER_PATH project.app/SharedSupport
SKIP_INSTALL NO
SOURCE_ROOT “/Volumes/Development/Project
SRCROOT “/Volumes/Development/Project
STRINGS_FILE_OUTPUT_ENCODING binary
STRIP_INSTALLED_PRODUCT YES
STRIP_STYLE all
SUPPORTED_DEVICE_FAMILIES 1,2
SUPPORTED_PLATFORMS “iphonesimulator
SYMROOT “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
SYSTEM_ADMIN_APPS_DIR /Applications/Utilities
SYSTEM_APPS_DIR /Applications
SYSTEM_CORE_SERVICES_DIR /System/Library/CoreServices
SYSTEM_DEMOS_DIR /Applications/Extras
SYSTEM_DEVELOPER_APPS_DIR /Developer/Applications
SYSTEM_DEVELOPER_BIN_DIR /Developer/usr/bin
SYSTEM_DEVELOPER_DEMOS_DIR “/Developer/Applications/Utilities/Built
SYSTEM_DEVELOPER_DIR /Developer
SYSTEM_DEVELOPER_DOC_DIR “/Developer/ADC
SYSTEM_DEVELOPER_GRAPHICS_TOOLS_DIR “/Developer/Applications/Graphics
SYSTEM_DEVELOPER_JAVA_TOOLS_DIR “/Developer/Applications/Java
SYSTEM_DEVELOPER_PERFORMANCE_TOOLS_DIR “/Developer/Applications/Performance
SYSTEM_DEVELOPER_RELEASENOTES_DIR “/Developer/ADC
SYSTEM_DEVELOPER_TOOLS /Developer/Tools
SYSTEM_DEVELOPER_TOOLS_DOC_DIR “/Developer/ADC
SYSTEM_DEVELOPER_TOOLS_RELEASENOTES_DIR “/Developer/ADC
SYSTEM_DEVELOPER_USR_DIR /Developer/usr
SYSTEM_DEVELOPER_UTILITIES_DIR /Developer/Applications/Utilities
SYSTEM_DOCUMENTATION_DIR /Library/Documentation
SYSTEM_LIBRARY_DIR /System/Library
TARGETED_DEVICE_FAMILY 1
TARGETNAME Project
TARGET_BUILD_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
TARGET_NAME Project
TARGET_TEMP_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
TEMP_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
TEMP_FILES_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
TEMP_FILE_DIR “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
TEMP_ROOT “/Users/username/Library/Developer/Xcode/DerivedData/project-dxdgjvgsvvbhowgjqouevhmvgxgf/ArchiveIntermediates/Project
TEST_AFTER_BUILD NO
UID 501
UNLOCALIZED_RESOURCES_FOLDER_PATH project.app
USER username
USER_APPS_DIR /Users/username/Applications
USER_HEADER_SEARCH_PATHS project/libs
USER_LIBRARY_DIR /Users/username/Library
USE_DYNAMIC_NO_PIC YES
USE_HEADERMAP YES
USE_HEADER_SYMLINKS NO
VALIDATE_PRODUCT YES
VALID_ARCHS “armv6
VERBOSE_PBXCP NO
VERSIONPLIST_PATH project.app/version.plist
VERSION_INFO_BUILDER username
VERSION_INFO_FILE project_vers.c
VERSION_INFO_STRING “\”@(#)PROGRAM:project
WRAPPER_EXTENSION app
WRAPPER_NAME project.app
WRAPPER_SUFFIX .app
XCODE_APP_SUPPORT_DIR /Developer/Library/Xcode
XCODE_PRODUCT_BUILD_VERSION 4B110
XCODE_VERSION_ACTUAL 0410
XCODE_VERSION_MAJOR 0400
XCODE_VERSION_MINOR 0410
YACC /Developer/usr/bin/yacc

lldb tips

Commands can have three forms:
Discoverable form: expression –object-description — foo
Abbreviated from  : e -O — foo
Alias: po foo

Breakpoints

b MyView.m: 4
breakpoint set –file MyView.m –line 4

b “-[MyViewA drawRect:]”
breakpoint set –name “-[MyViewA drawRect:]”

b drawRect:
breakpoint set –selector drawRect:

br co a / breakpoint command add  Breakpoint commands run each time a breakpoint is hit
> p rect expression rect
> bt thread backtrace
> c process continue
> DONE

p id $myModel = self  expression id $myModel = self // Create a presistent variable of type id
b “-[MyModel dealloc]”
br m -c “self == $myModel” breakpoint modify –condition “self == $myModel”

Watch Points 4 on Intel, 2 on ARM
w s v self->_needsSynchronization
watchpoint set variable self->_needsSynchronization

Stepping can hit breakpoints
LLDB maintains a stack of things you are doing
When you step, LLDB puts it on the stack
If you hit a breakpoint LLDB remembers the stack
So, when hit continue, LLDB will get you back to where when you hit step

Stepping repeatedly over irrelevant code gets old quickly
th u 11
thread until 11 

Call the code using Clang
b “-[ModelDerived removeDuplicates]”
e -i false — [self removeDuplicates]
expression –ignore-breakpoints false — [self removeDuplicates]

Data Formatters
Xcode 5 can show memory summary in debug area, It has builtin DataFormatters for Foundation CoreFoundation and STL
http://lldb.llvm.org/varformats.html
LLDB pass SBValue to formatter

def MyAddress_Summary(value, unused):
    firstName = value.GetChildMemberWithName("_firstName")
    lastName = value.GetChildMemberWithName("_lastName")
    firstNameSummary = firstName.GetSummary()
    lastNameSummary = lastName.GetSummary()
    return firstNameSummary + " " + lastNameSummary

ty su a MyAddress -F MyAddress_Summary

Define Types in Console
expression
typedef $NoOpaque {
int foo; int bar
}
($NoOpaque)Opaque

Custom LLDB Commands

SB stands for Script Bridge
def MyCommand_Impl(debugger, user_input, result, unused)
co sc a foo -f foo

thread = debugger.GetSelectedTarget().GetProcess().GetSelectedThread()
for frame in thread.frames:
    if frame.function.name == "MyFunction":
        # update counters
    print >>result, "depth: " + str(depth)

~/.lldbinit loaded at debugger startup
~/.lldbinit-Xcode

Xcode 4 无法打开 Xcode 5 DP 打开过的工程文件 解决方法

试用Xcode 5 DP打开现有工程文件后再用Xcode4打开后 Xcode 4 会进入崩溃模式
折腾了几次发现下面的方法可以让工程文件恢复

如果在Xcode5-DP中打开过xib文件需要在侧栏中修改Interface Builder Document下的Open in为Xcode 4.6, 然后Clean,关掉工程,再到DerivedData中删掉对应的文件夹

如果遇到打开xib还会崩的话可以找一个没有用Xcode5-DP打开过的xib把两个文件用文本编辑器打开,对比着把前几行的版本号修改一下

另外还可以尝试删除 .xcodeproject中的.xcworkspace 与 xcuserdata

还有打开后4下断点失效的问题,可以试试设置link-time optimization为NO

create cgcontext

创建一个 CGContext 的方法

CG_INLINE CGContextRef CGContextCreate(CGSize size)

{

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGContextRef ctx = CGBitmapContextCreate(nil, size.width, size.height, 8, size.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast);

CGColorSpaceRelease(space);

return ctx;

}

CG_INLINE UIImage* UIGraphicsGetImageFromContext(CGContextRef ctx)

{

CGImageRef cgImage = CGBitmapContextCreateImage(ctx);

UIImage* image = [UIImage imageWithCGImage:cgImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];

CGImageRelease(cgImage);

return image;

}