如何在scala中的对象内使用方法?(how to use method inside an object in scala?)
我在scala工作表中有这个示例代码。 我不明白为什么我无法访问代码底部的函数。
object chapter5 { println("Welcome to the Scala worksheet") trait Stream2[+A] { def uncons: Option[(A, Stream2[A])] def isEmpty: Boolean = uncons.isEmpty } object Stream2 { def empty[A]: Stream2[A] = new Stream2[A] { def uncons = None } def cons[A](hd: => A, tl: => Stream2[A]): Stream2[A] = new Stream2[A] { lazy val uncons = Some((hd, tl)) } def apply[A](as: A*): Stream2[A] = if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*)) } val s = Stream2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) s.isEmpty //I can access to the function declared in the trait s.cons // error // I can not access to the function declared in the object }
另外我需要编写toList方法。 我应该在哪里写呢? 如果我无法访问这些方法,我该如何测试呢?
I have this sample code in a scala worksheet. I do not understand why I can not access to the functions as at the bottom of the code.
object chapter5 { println("Welcome to the Scala worksheet") trait Stream2[+A] { def uncons: Option[(A, Stream2[A])] def isEmpty: Boolean = uncons.isEmpty } object Stream2 { def empty[A]: Stream2[A] = new Stream2[A] { def uncons = None } def cons[A](hd: => A, tl: => Stream2[A]): Stream2[A] = new Stream2[A] { lazy val uncons = Some((hd, tl)) } def apply[A](as: A*): Stream2[A] = if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*)) } val s = Stream2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) s.isEmpty //I can access to the function declared in the trait s.cons // error // I can not access to the function declared in the object }
In addition I need to write the toList method. Where should I write it? How can I test it if I can not access to the methods ?
原文:https://stackoverflow.com/questions/28860279
最满意答案
您需要调用
[cell.detailTextLabel sizeToFit]
以使标签在cellForRowAtIndexPath中实际调整大小。 它不会因为您将numberOfLines设置为0而自行调整大小。请参阅此问题并阅读其答案以获得更多说明。Turns out I just needed to enable all of the Autosizing options in interface builder for the label.
相关问答
更多-
如果您使用Core Text绘制文本,则应使用Core Text计算文本的高度。 查看CTFramesetterSuggestFrameSizeWithConstraints函数。 If you're using Core Text to draw the text, you should use Core Text to calculate the height of the text. Check out the CTFramesetterSuggestFrameSizeWithConstraints ...
-
它也适用于多行字符串。 基本上它的一个功能可以让您看到使用字体和换行符模式进行渲染时,字符串的大小有多大。 我使用它在我的应用程序中的几个点,当我有一个可变长度的文本我想要显示在一定的区域。 默认情况下,UILabel将垂直居中。 要使文本顶部对齐,您需要将标签的大小设置为仅在其中的字符串所需的高度。 我使用这种方法来做到这一点。 而我将如何使用它的例子如下: //Calculate the expected size based on the font and linebreak mode of your ...
-
你可以试试这个: NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]}; // NSString class method: boundingRectWithSize:options:attributes:context is // available only on ios7.0 sdk. CGRect rect = [textToMeasure boundingR ...
-
CGFloat对应于C float数据类型; 正确的格式说明符是%f : NSString *text = @"A really long string in here"; CGSize theSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; NSS ...
-
为什么NSString sizeWithFont:返回错误的大小?(Why does NSString sizeWithFont: return the wrong size?)[2023-04-02]
@ Vladimir的评论是正确的我认为 - NSString没有考虑UITextField的边距。 您可以尝试获取光标前的文本并创建仅包含该文本的新UITextField。 然后,如果在新文本字段上调用[tempTextField sizeToFit] ,那么它的边界应该是您要查找的大小。 @Vladimir's comment is right I think - an NSString doesn't take into account the margins of a UITextField. Yo ... -
好消息! 有一种“计算机科学/数学方法”可以更快地完成这项工作。 您链接到的示例执行线性搜索:它只是从字符串末尾一次切换一个字符,直到它足够短。 因此,它所花费的时间将与字符串的长度成线性比例,而对于长字符串,它将非常慢,正如您所发现的那样。 但是,您可以轻松地将二进制搜索技术应用于字符串。 你可以从中间开始,而不是从最后开始并一次放下一个角色: THIS IS THE STRING THAT YOU WANT TO TRUNCATE ^ 你计算“这就是那个字 ...
-
使用sizeWithFont:constrainedToSize:lineBreakMode:相反。 NSString * t = @"
"; CGSize constrainSize = CGSizeMake(285, MAXFLOAT); CGSize size = [t sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:constrainSize lineB ... -
你的字符串的内容和字体的大小是什么? 在我看来,它正如您所期望的那样包装单词并返回包含结果所需的最小大小。 请注意,您的Out宽度仅比In的宽度少25个点。 除非您的字符串恰好包裹在360点,否则您希望得到的宽度小于约束大小。 What is the contents of your string and the size of your font? It seems to me that it's wrapping words as you expected and returning the minim ...
-
首先,你必须覆盖在Indexpath处的行的高度,并通过函数计算你的字符串“ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *cellText; cellTextHeight = [self heightOfCellForText:cellText]; return cellTextHeight+margin; ...
-
您需要调用[cell.detailTextLabel sizeToFit]以使标签在cellForRowAtIndexPath中实际调整大小。 它不会因为您将numberOfLines设置为0而自行调整大小。请参阅此问题并阅读其答案以获得更多说明。 Turns out I just needed to enable all of the Autosizing options in interface builder for the label.