我正在浏览我的一个应用程序中的一些旧代码,并在可能存在问题的领域修复代码.
我看到很多旧代码使用...
NSRange range = //determine range here....
if(range.length > 0)
{
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
该代码是"罚款",还是应该将其改为此?
NSRange range = //determine range here....
if(range.location != NSNotFound)
{
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
这两种方法基本上是否相同?
我见过很多与代表有关的帖子,我想知道引用它们的正确方法.假设我有一个声明为的对象:
@interface MyViewController : UITableViewController {
id delegate;
}
@property (nonatomic, retain) id delegate;
@end
Run Code Online (Sandbox Code Playgroud)
在生命周期中MyViewController,它将响应与用户的交互来调用其委托的方法.
什么时候摆脱一个实例MyViewController,是否delegate需要release在实现的dealloc方法中使用ivar,因为它被声明了retain?
或者相反,delegate甚至应该保留?也许它应该是@property (nonatomic, assign) id delegate?根据Apple的文档:
retain ...您通常将此属性用于标量类型(如NSInteger和CGRect),或者(在引用计数环境中)用于您不拥有的对象(如委托).
通常情况下,我只是按照文档的说法去做,但我看到很多代码都会调用retain代理.这只是"糟糕的代码吗?" 我在这里请专家......处理这个问题的正确方法是什么?
我正在使用页面视图控制器来翻阅一系列视图控制器,每个视图控制器都是从故事板中实例化的.我基本上使用基于标准页面的应用程序代码作为基础,并构建了我需要的东西.我正在翻阅的UITableViewController视图控制器是包含自定义滚动视图的子类或自定义视图控制器.
页面视图控制器嵌入在导航控制器中,但是没有一个视图控制器遵循顶部布局指南,即使在我的自定义视图控制器的情况下约束被设置到故事板中的布局指南,我认为表视图控制器将自动管理它.我的意思是视图的内容从(0.0,0.0)开始,而不是用户可以看到的地方.唯一有效的是实际上将视图控制器视图的帧设置为在状态栏+导航栏下面开始,但我希望滚动视图在透明导航栏下滚动.
我做错了什么或不做什么?
目前我正在通过apple reachability.m/.h使用该类,它可以工作,除了它通知我任何更改,我想只通知用户网络是否无法访问.目前,如果我有互联网连接,然后松散网络它告诉我.但是当你重新连接到网络时,它也告诉我,我不想要.我希望它只告诉我什么时候有丢失/没有网络.
我认为这与电话有关:
- (void)viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [[Reachability
reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName:
@"www.google.ca"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
}
Run Code Online (Sandbox Code Playgroud)
在调用时-[NSNotificationCenter addObserver:selector:name:object:],该名称是否具有任何其他功能,然后字面上是一个名称?这是我第一次使用NSNotificationCenter,所以我不太熟悉这个问题.
编辑:
这是我的checkNetworkStatus函数:(问题是我得到"NotReachable",因为网络连接回来了,NSAlert多次关闭)
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
UIAlertView * …Run Code Online (Sandbox Code Playgroud) 我有一长串一些角色.我想用其他字符替换一些字符.
例如
string1="Hello WORLD12";
string2="world";
string1= search string2 in string1 and replace it;
//need this method in objective c
string1="Hello world12";
Run Code Online (Sandbox Code Playgroud) 我一直在使用iOS中的自定义字体,并且除了自定义字体之外,大部分内容都不会显示在我的任何设备上.
[playMenuBtn.titleLabel setFont:[UIFont fontWithName:@"fontname" size:40]];使字体工作.当我在模拟器中运行我的应用程序时,它看起来很好并且有效.但是,只要我在物理设备上运行它,它就会显示默认大小的默认字体.
如果我使用:
UIFont *font = [UIFont fontWithName:@"VINCASTENCIL" size:20];
NSLog(@"Font:%@", font);
然后模拟器返回:
2012-04-07 01:10:41.544 AppName[47412:f803] Font:<UICFFont: 0x683e040> font-family: "VINCA STENCIL"; font-weight: normal; font-style: normal; font-size: 20px
设备返回的位置:
2012-04-07 00:42:56.358 AppName[5079:707] Font:(null)
我已经尝试了所有的东西,包括尝试FontBook告诉我字体可以命名的所有名称.我知道它是免费的,并且没有许可证.
任何帮助都会非常感激,因为它让我疯狂,我真的不想使用图像来解决这些问题,因为本地化会很痛苦!
我的应用程序与iOS5和iOS6兼容.到现在为止我使用时没有问题:
NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];
Run Code Online (Sandbox Code Playgroud)
现在使用iOS7并且uniqueIdentifier不再工作了,我改为:
NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Run Code Online (Sandbox Code Playgroud)
问题是,这对iOS5不起作用.
如何实现与iOS5的向后兼容?
我试过这个,没有运气:
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
// iOS 6.0 or later
NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
#else
// iOS 5.X or earlier
NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];
#endif
Run Code Online (Sandbox Code Playgroud) 我已经设置了一个nsurl,它从http中获取数据.当我运行仪器时,它说我有一个泄漏的NSFNetwork对象.
我如何释放theConnection在(无效)ButtonClicked?或者它会在稍后发布?
- (void)ButtonClicked {
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:KmlUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0f];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// receivedData is declared as a method instance elsewhere
NSMutableData *receivedData = [[NSMutableData data] retain];
[self setKMLdata:receivedData];
} else {
// inform the user that the download could not be made
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the receivedData
// receivedData is declared as a method instance …Run Code Online (Sandbox Code Playgroud) memory-leaks memory-management objective-c nsurlconnection nsurlrequest
我添加了一个自定义字体 - 我从网上下载的字体.它是TTF格式,可以在我的Mac上的其他程序上使用.(已成功将其添加到字体书并用于烟花).
我已将密钥添加到信息.plist中,但字体似乎不起作用.
当我NSLog字体它是空的所以我假设它没有找到它.
TTF名为LONDON __.TTF,但名称为伦敦字体.
我应该使用什么作为字体并添加到info.plist为此工作?
谢谢
担
应用程序字体资源路径= LONDON __.TTF
NSLog(@"%@",[UIFont fontWithName:@"London font" size:22]);
Run Code Online (Sandbox Code Playgroud)
和
NSLog(@"%@",[UIFont fontWithName:@"LONDON__.TTF" size:22]);
他们两个都返回Null.
虽然该文件名为LONDON __.TTF,但字体在Font Book中显示为每个人.这是问题吗?
ios ×9
objective-c ×7
iphone ×4
cocoa-touch ×3
nsstring ×2
alamofire ×1
custom-font ×1
fonts ×1
ios7 ×1
memory-leaks ×1
nsrange ×1
nsurlrequest ×1
reachability ×1
swift ×1
swift5 ×1
uifont ×1