mam ten kod napisany w Objective C:Jak napisać ten kod w szybkim tempie?
NSRect textRect = NSMakeRect(42, 35, 117, 55);
{
NSString* textContent = @"Hello, World!";
NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
textStyle.alignment = NSCenterTextAlignment;
NSDictionary* textFontAttributes = @{NSFontAttributeName: [NSFont fontWithName: @"Helvetica" size: 12], NSForegroundColorAttributeName: NSColor.blackColor, NSParagraphStyleAttributeName: textStyle};
[textContent drawInRect: NSOffsetRect(textRect, 0, 1 - (NSHeight(textRect) - NSHeight([textContent boundingRectWithSize: textRect.size options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes]))/2) withAttributes: textFontAttributes];
}
Teraz chcę napisać ten kod SWIFT. To, co mam do tej pory:
let textRect = NSMakeRect(42, 35, 117, 55)
let textTextContent = NSString(string: "Hello, World!")
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.CenterTextAlignment
let textFontAttributes = [NSFontAttributeName: NSFont(name: "Helvetica", size: 12), NSForegroundColorAttributeName: NSColor.blackColor(), NSParagraphStyleAttributeName: textStyle]
textTextContent.drawInRect(NSOffsetRect(textRect, 0, 1 - (NSHeight(textRect) - NSHeight(textTextContent.boundingRectWithSize(textRect.size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes)))/2), withAttributes: textFontAttributes)
ta linia jest źle:
let textFontAttributes = [NSFontAttributeName: NSFont(name: "Helvetica", size: 12), NSForegroundColorAttributeName: NSColor.blackColor(), NSParagraphStyleAttributeName: textStyle]
Co jest złego w tym wierszu?
Jest to błąd z kompilatora:
„Nie można znaleźć przeciążenie dla«init», który akceptuje dostarczonych argumentów”.
Co jest nie tak z tą linią? Xcode powinien pokazać ci jakiś błąd. –
Nie można znaleźć przeciążenia dla "init", który akceptuje podane argumenty. To jest błąd. Próbowałem użyć mniej atrybutów, ale otrzymałem ten sam błąd. –