如何从具有相同名称的嵌套类型的类中引用全局类型?

时间:2015-12-20 10:39:57

标签: swift scope swift2 nested-class scoping

我有一个在全局范围内声明的类,另一个具有相同名称的类嵌套在某个类中。

class Address {
    var someProperty: String?
}

class ThirdPartyAPI {
    class Address {
        var someOtherProperty: String?
        init(fromAddress address: Address) {
            self.someOtherProperty = address.someProperty
        }
    }
}

问题是:如何从初始化中引用全局类而不是内部类?在给出的示例中,我得到了错误Value of type 'ThirdPartyAPI.Address' has no member 'someProperty',这意味着编译器引用内部Address而不是全局内容<Page.Resources> <DataTemplate x:Key="ChatTemplateR"> <Grid HorizontalAlignment="Stretch"> <StackPanel HorizontalAlignment="Right" > <Border Background="{ThemeResource SystemControlBackgroundAccentBrush}" > <TextBlock MinWidth="200" Text="{Binding conversation}" TextWrapping="Wrap" Margin="5"/> </Border> <Path x:Name="DownRightTri" HorizontalAlignment="Right" Margin="0,0,10,0" Fill="{ThemeResource SystemControlBackgroundAccentBrush}" Data="M0,0 H10 V10" /> </StackPanel> </Grid> </DataTemplate> <DataTemplate x:Key="ChatTemplateL"> <Grid HorizontalAlignment="Stretch"> <StackPanel HorizontalAlignment="Left"> <Path x:Name="UpLeftTri" HorizontalAlignment="Left" Margin="10,0,0,0" Fill="{ThemeResource SystemControlBackgroundAccentBrush}" Data="M0,-5 V5 H10 " /> <Border Background="{ThemeResource SystemControlBackgroundAccentBrush}" > <TextBlock MinWidth="200" Text="{Binding conversation}" TextWrapping="Wrap" Margin="5"/> </Border> </StackPanel> </Grid> </DataTemplate> <local1:ChatTemplateSelector x:Key="ChatSelector" LeftTemplate="{StaticResource ChatTemplateL}" RightTemplate="{StaticResource ChatTemplateR}"/> </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button x:Name="backButton" FontFamily="Segoe MDL2 Assets" Content="&#xE0E2;" Foreground="{StaticResource SystemControlBackgroundAccentBrush}" FontSize="25" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Click="backButton_Click"> <Button.Transitions> <TransitionCollection> <EdgeUIThemeTransition Edge="Left"/> </TransitionCollection> </Button.Transitions> </Button> <TextBlock Text="Chats" Grid.Column="1" Style="{ThemeResource tb1}" HorizontalAlignment="Center"/> <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <ListView x:Name="lv" ItemsSource="{Binding BuddyChatOC}" ItemTemplateSelector="{StaticResource ChatSelector}"> </ListView> <RelativePanel Grid.Row="1" Margin="5,10,5,10"> <TextBox x:Name="sendtext" Margin="0,0,2,0" RelativePanel.AlignLeftWithPanel="True" RelativePanel.LeftOf="sendtextbutton"/> <Button x:Name="sendtextbutton" Content="Send" Command="{Binding sendChatCommand}" RelativePanel.AlignRightWithPanel="True" > </Button> </RelativePanel> </Grid> </Grid>

2 个答案:

答案 0 :(得分:8)

使用typealias

class Address {
    var someProperty: String?
}

typealias GlobalAddress = Address

class ThirdPartyAPI {
    class Address {
        var someOtherProperty: String?
        init(fromAddress address: GlobalAddress) {
            self.someOtherProperty = address.someProperty
        }
    }
}

答案 1 :(得分:3)

您可以通过添加模块名称来唯一地引用类型。 所以,如果

class Address {
    var someProperty: String?
}

在“MySuperApp”中定义,然后您可以将其称为 MySuperApp.Address

class ThirdPartyAPI {

    class Address {
        var someOtherProperty: String?
        init(fromAddress address: MySuperApp.Address) {
            self.someOtherProperty = address.someProperty
        }
    }
}

(但如果您有选择,那么尽量避免产生歧义 您的代码更容易理解。)