我正在处理旧代码,我有这条警告消息: 按值传递的struct参数包含未初始化的数据(例如,通过字段链:'origin.x')。如果我能获得圆顶帮助,我将非常感激:)
我正在使用的代码:
- (void)positionScroller
{
CGRect screenFrame = [[UIScreen mainScreen] bounds];
CGRect scrollerRect;
if( self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
{
scrollerRect = CGRectMake( 0, 0, screenFrame.size.width, screenFrame.size.height );
}
else if( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
scrollerRect = CGRectMake( 0, 0, screenFrame.size.height, screenFrame.size.width );
}
_scroller.frame = scrollerRect; <---This is where the compiler gives the warning
}
最诚挚的问候。
答案 0 :(得分:7)
问题是编译器无法确定是否曾到达过if / else-if块之一,在这种情况下,scrollerRect
仍未被初始化。您应该添加纯else
语句或初始化scrollerRect
,例如将其设置为CGRectZero
。
顺便说一句,这与内存泄漏无关,更多的是逻辑错误。
答案 1 :(得分:4)
您可以轻松摆脱这样的警告:
- (void)positionScroller
{
CGRect screenFrame = [[UIScreen mainScreen] bounds];
CGRect scrollerRect;
if( self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
{
scrollerRect = CGRectMake( 0, 0, screenFrame.size.width, screenFrame.size.height );
}
else
{
scrollerRect = CGRectMake( 0, 0, screenFrame.size.height, screenFrame.size.width );
}
_scroller.frame = scrollerRect; <---This is where the compiler gives the warning
}
答案 2 :(得分:2)
您已声明CGRect
CGRect scrollerRect;
在检查了一些条件后,你已经为此分配了价值。如果两个条件都失败,那么它将没有任何价值。所以它正在发出警告。因此,请添加else
条件并为scrollerRect
分配值。
所以你可以拥有
if( self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
{
scrollerRect = CGRectMake( 0, 0, screenFrame.size.width, screenFrame.size.height );
}
else if( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
scrollerRect = CGRectMake( 0, 0, screenFrame.size.height, screenFrame.size.width );
}
else
{
scrollerRect = CGRectZero;
}