Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将结构声明为指针,以便检查它是否已初始化_Objective C_C_Pointers_Struct_Null - Fatal编程技术网

Objective c 将结构声明为指针,以便检查它是否已初始化

Objective c 将结构声明为指针,以便检查它是否已初始化,objective-c,c,pointers,struct,null,Objective C,C,Pointers,Struct,Null,在下面的代码中,我想知道viewTransform结构是否已初始化,以确定是否旋转视图。所以我把viewTransform做成了一个指针,而不仅仅是一个正则变量 这是一个好的编程实践,还是有一些潜在的警告 如果必须,我可以声明一个BOOL,以跟踪viewTransform是否已初始化 - (void)deviceOrientationDidChange { UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]

在下面的代码中,我想知道
viewTransform
结构是否已初始化,以确定是否旋转视图。所以我把
viewTransform
做成了一个指针,而不仅仅是一个正则变量

这是一个好的编程实践,还是有一些潜在的警告

如果必须,我可以声明一个
BOOL
,以跟踪
viewTransform
是否已初始化

- (void)deviceOrientationDidChange
{
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

    CGAffineTransform *viewTransform = NULL;

    if (deviceOrientation == UIDeviceOrientationPortrait)
    {
        *viewTransform = CGAffineTransformIdentity;
    }
    else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)
    {
        *viewTransform = CGAffineTransformMakeRotation(M_PI * 1.0f);
    }
    else if (deviceOrientation == UIDeviceOrientationLandscapeLeft)
    {
        *viewTransform = CGAffineTransformMakeRotation(M_PI * -0.5f);
    }
    else if (deviceOrientation == UIDeviceOrientationLandscapeRight)
    {
        *viewTransform = CGAffineTransformMakeRotation(M_PI * 0.5f);
    }

    if (viewTransform != NULL)
    {
        for (UIView *view in self.autoRotateViews)
        {
            [view setTransform:(*viewTransform)];
        }
    }
}

这会像地狱一样崩溃。您将viewTransform声明为NULL,然后取消引用它。相反,不要让它成为指针。使用

CGAffineTransform viewTransform;
相反,是的,用BOOL跟踪初始化状态

如果你真的想避免使用BOOL而导致事情过于复杂,你也可以这样做:

CGAffineTransform *viewTransform = NULL;
if (deviceOrientation == UIDeviceOrientationPortrait)
{
    viewTransform = malloc(sizeof(*viewTransform));
    NSAssert(viewTransform != NULL, @"malloc() failed");
    *viewTransform = CGAffineTransformIdentity;
}
然后检查viewTransform是否为NULL(未初始化)或非NULL(已初始化)


如果您选择这种(过分复杂的)方法,请不要忘记在使用后释放转换指针。

H2CO3是正确的,您所编写的将崩溃

您可以直接使用tack
else返回在if-else if-。。。链,以避免整个需要检查转换是否已设置


编辑以澄清:当然,我指的是作为使用指针的替代方法的上述内容。因此,我建议您将
viewTransform
还原为
CGAffineTransform
,而不是指针,如果您没有将其设置为任何值,则可以提前返回。

否?没有公认的答案?
    switch (orientation) {
    case UIDeviceOrientationLandscapeLeft:
        t = CGAffineTransformMakeRotation(M_PI_2);
        break;
    case UIDeviceOrientationLandscapeRight:
        t = CGAffineTransformMakeRotation(-M_PI_2);
        break;
    case UIDeviceOrientationPortraitUpsideDown:
        t = CGAffineTransformMakeRotation(M_PI);
        break;
    case UIDeviceOrientationPortrait:
        t = CGAffineTransformIdentity;
        break;
    default:
        return;
}